Entity Framework CodeFirst table pluralization - entity-framework-4.1

I'm using CodeFirst of EF with a well defined Database.
My Database has a table named 'Centros' (Portuguese word) and I manage to find that EF tries to pluralize my entities to get a 'Centroes' witch is wrong in this case.
If I remove the pluralization modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); i manage to get it to work BUT I have to rename my table to 'Centro' (to match my entity name).
In Portuguese 'Centro' is singular, 'Centros' is plural.
I don't want to rename my table names so how can I specify the matching table name for my entity after a remove the pluralization convention?

I annotate all my classes, regardless of whether the framework can do it for me through some smart reflection routines. E.g.
[Table("Order")]
public class Order
{
}
We can touch type, it's cleaner and it's less likely to fall over in some unexpected event.

Or you can do one at a time like this in Fluent API:
modelBuilder.Entity().ToTable("ContactInfo");

ModelBuilder is now System.Data.Entity.DbModelBuilder.

Related

Symfony 4 - How to dynamically add field in an entity?

I want to have a form where I can add new fields (columns) in an specific entity. Is there a function for this?
Kind regards
Adding a whole column to the table through an HTML form is a weird use case.
If you want to stick to the ORM way of managing the persisted data, you'll have to dynamically add properties to existing entities, which might be a sign of bad schema design.
What I would guess you probably need is an automated way to add this column to your Entity. In such a case I would use the maker bundle.
Supposing that your Entity is called Employee, all you have to do is to type in the following command:
bin/console make:entity
When you'll be asked for the Entity name, enter Employee. The interpreter will tell you that this entity exists and if you want to extend it with news fields, and there you go.

How do you exclude class fields from SORM table definition?

I'm persisting a class to MySQL using SORM. This class contains one or two fields that I need on the class but are not to be persisted to the DB. Does anyone know how to exclude these from the table definition?
The problem is that the type of the field is unsupported by SORM. Not a problem, as it's an actor ref and shouldn't be persisted, but there doesn't appear to be a simple way to tell SORM to just ignore it.
Thanks.
There is no way to ignore fields in SORM.
In fact it is a very bad idea from a design perspective to mix a business logic (which an actor ref definitely is) into a model. A model should be just data, nothing else. Even if SORM didn't require that, I would still recommend you to extract the actor-related logic into another object.

Entity Framework 4.1 with existing classes AND existing tables

I have a large set of classes implementing business logic. Most have a .Load method that uses plain old ADO.net to read values from Sql Server that I've written by hand over the years. This all predates Linq2Sql and EF.
Now I want to update my class library to use Entity Framework but I'd like to do it as painlessly as possible. I've learned EF can infer column and key names from property names in my classes, but my classes have many properties that don't correspond to column names and some properties that don't match the database's column names. I'd prefer not to have to .Ignore() each of those properties (and remember to always .Ignore() any future properties) and .HasColumnName() all the differences.
What's the easiest way to use EF with existing tables AND existing classes so I can do minimal mappings and still use a DbContext to .Find() an entity and SaveChanges() and all the other nice strongly-typed things EF supports without going through all my business classes by hand and annotating which properties to include?
For example, I'd expect to be able to db.Customers.Find(123) and have it create a Customer instance, select * from customers where CustomerID=123, and map the columns that DO exist to the properties that DO exist as best as possible and give me a ready to use Customer instance and then I can annotate any differences as needed. Is this possible or am I asking too much of EF?
Is there maybe a smarter DbContext that will make a best effort to map properties so I can leverage all my existing business classes? Maybe I should consider some other ORM?
Try this:
create a data model (.edmx) from your database.
edit the model, Adding property and procedures of your Class That You want to add to the database.
Finally, update your database from your model (.Edmx) Selecting only the tables and procedures exist That You Have changes.
You can look at those tutorials
http://msdn.microsoft.com/en-us/data/gg685494
http://msdn.microsoft.com/en-us/data/gg685489
EF4 “Code First” enables you to optionally override its default database persistence mapping rules, and configure alternative ways to map your classes to a database.
There are a few ways to enable this. One of the easiest approaches is to override the “OnModelCreating” method defined on the DbContext base class:
public class YourContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Mapping
}
}
You can still search your entities by Primary Key by using Find:
var unicorn = context.Unicorns.Find(3);

Auto generate DB with entity framework model first design

I have used both the EF code first and model first apporaches. In my case the model first approach has overall been better for my use. The only thing I miss from code first is that it would auto generate the mappings, and then use those mappings to create a database. Is their any way that I can get model first to do this same thing, namely create the mappings automatically when it instantializes the database.
The EDMX designer should handle this already. When you first generate a database from your model, EF will create the appropriate mappings for you.

Define a one-to-one relationship with LinqToSQL

I'm playing around with LinqToSQL using an existing multi-lingual database, but I'm running into issues mapping a fairly important one-to-one relationship, so I suspect I am using the feature incorrectly for my database design.
Assume two tables, Category and CategoryDetail. Category contains the CategoryId (PK), ParentId and TemplateId. CategoryDetail contains the CategoryId (FK), LanguageId, Title and Description (in the appropriate language), with a combined PK of CategoryId and LanguageId.
If I drag-and-drop these tables into the LinqToSQL designer, the resultant object model has Category with a collection of CategoryDetail objects, which should never be the case. I'd like to be able to filter on LanguageId at the DataContext level, meaning that the whole Category is encapsulated within Category.CategoryDetail, not all language version encapsulated within Category.CategoryDetails.
This database worked fine on my old object library (an old-school custom BOL and DAL), but I fear that LinqToSQL would require this to change in order to give me the required result.
What is the best way to make this relationship (and language filtering) as seamless as possible?
You can view properties of the association. (Right click on the line representing the association and show properties.) The properties will tell you if it is a one-to-one or one-to-many relationship. This is reflected in code by having either a single entity association (one-to-one) or an entity set association (one-to-many).
I would have to assume cant be a true 1 to 1. Sounds like you have a PK of CatID and Lang ID on the Cat Details table. That would explain why its putting a collection. I could be wrong as you didnt mention the PK's of the CatDetails table
EDIT: A combined Pk of CatID and Lang ID makes that a 1:m relationship, and Linq to SQL is actually doing the correct thing. The only way it could possibly be a true 1:1 is if you had a lang ID on the cat table as well and that was part of the FK. I htink you may have to rethink what you want to do, or how you want to implement it.
I think LINQ to SQL models the database structure directly.
You have two tables so it creates 2 objects.
Have you had a look at LINQ to Entities this allows you to create another layer above the database strucure to make for more readable classes.
Since you don't have a 1:1 relationship the mapping alone will not provide the desired functionality. However it is easy to provide a method in the parent auto-generated class that does the job:
public partial class Category
{
public IEnumerable<CategoryDetail> GetDetailsByLanguage(string langID)
{
return this.CategoryDetails.Where(c => c.LangID == langID);
}
}