For some reason I have most of my foreign keys named like "ID" + ForeignTableName.
I can't figure out yet if this naming respects any of the foreign key naming conventions built into EF6.
If it does could someone please indicate the actual convention? If not I would very much appreciate some code to configure this convention into modelBuilder (to be more specific I saw some examples on how to configure the primary key convention in the model builder without creating another Convention class, can it be done likewise for foreign keys?)
Related
I am creating models in Entity Framework. I am trying to insert records in tblOffStrategy. In tblOffStrategy, OffId is Foreign Key and OffId and BonusStrategy are composite keys. DBA's creates table for us so I am creating model based on tables.
When I add-migration from Package Manager Console, I am getting "Unable to determine composite primary key ordering, Use the ColumnAttribute". Also I am not sure if my Foreign Key relationship is working.
Would anyone take a look at it and point me to right direction? Any help will be appreciated
I am developing a web app on an existing database which unfortunately I cannot change.
I get this error
"More than one property map found for property 'Id' when using case-insensitive search. "
After a little search I found that the problem is that there are some conventions in EF and more specifically one that states (according to what I read here) that:
if the field name contains a suffix of ID (case-insensitive), then EF will automatically include it as a primary key
The table has already a primary key (named 'id').
Is there a way to disable the EF conventions or in any other way to allow having besides my 'id' field, a field named "something_id" without getting this error?
this is how to disable the id convention of EF:
inside OnModelCreating
modelBuilder.Conventions.Remove<IdKeyDiscoveryConvention>();
Using EF code first, we have a db seeding framework that uses dependency injected Db initializers and seeders to populate a dev db with sample test data.
Some of these seeding operations need to import a lot of data, so for a couple of tables, we use actual SQL files with INSERT statements. For some of those insert statements, foreign keys must be diabled then re-enabled:
ALTER TABLE [Schema].[TableName] NOCHECK CONSTRAINT [TableName_FkPropertyName]
-- perform a block of inserts
ALTER TABLE [Schema].[TableName] CHECK CONSTRAINT [TableName_FkPropertyName]
I just updated from EF 4.2 to EF 4.3 and noticed that these no longer work. An examination of the db created by EF shows that the FK's are now named differently:
FK_CodeFolder1.Table1Name_CodeFolder2.Table2Name_DbFkColumnName
Is there any way to remove this naming convention and go back to the original? If not, how is this not a known issue or breaking change?
Update after Ladislav's Reply
Ladislav is right, my above explanation of the new naming pattern was not quite right. I have updated it. The part preceding the . was not the full namespace, but it was the name of a folder in the entity model project. So if I had an entity WidgetAbc in folder AggregateSet1, the fk pattern fragment would be AggregateSet1.WidgetAbc, not just WidgetAbc.
Why do you think it is an issue or breaking change? IMHO it is EF internal behavior - you are using code first and in this approach you were not supposed to work directly with database and especially you were not supposed to base your custom database scripts on hardcoded names because you don't have control over their generation.
I don't think you can revert the behavior but you can start using migrations and code your table definitions - AddForeignKey method should allow you naming your FK constraint.
Btw. I see different naming pattern of FK constraints in EFv4.3:
FK_DependentTableName_PrincipalTableName_FKColumnName
I have noticed some strange behavior when it comes to nullable foreign keys in my database. I have just started playing with the entity framework so I'm probably doing something wrong, but I cannot figure this one out.
Say I have the following two tables: (CountryID is a foreign key that is nullable)
When I create a new entity model I end up with this:
But the CountryID properties are set to Nullable (None) - instead of True. Obviously this is a really trivial example, but with a large database it would be difficult switch all of these manually. Is there any way to have the entity framework use a nullable int? for these foreign keys?
I am learning code first and I have project to be used with an existing database.
I am a bit confused of what I meant to be doing.I will explain:
Do I need to create an entityconfiguration for each table in my existing database?
Within this EntityConfiguration for each table do I need to create foreign key relationships?
Do I need to do a ToTable for each table in my existing database?
Is there any free tool "codeplex" that pointing to an existing db will generate this codeFirst stuff?
I have seem few blogs about "EF Code first with existing db" but I am not sure or was not clear to me If Need to create this stuff or I will be getting strange errors like "MyColumn_MyColum" basically as if codeFirst is trying to build some FKs or something.
can somebody clarify?
thanks a lot. I know there are few questions but if you can answer 01 or 2 that would be fine.
thanks again
If you want the code to be generated for you use database-first approach with DbContext API. Simply create EDMX file from your database and let DbContext Generator template generate all entities and context for you.
DbContext Fluent API is mainly targeted to the code-first development approach where EF will created database for you from the code you provided. It can be used with existing database but it requires much more skills and understanding of mapping wich EF can provide to you.
Generally:
You don't need to provide EntityConfiguration for each table if you follow some naming conventions (entity name is singular form of table name, all properties have the same name, primary key in table and entity is named as Id or EntityNameId, etc.).
You don't need to define relationships manually if you follow conventions with exposing navigation properties and possibly also foreign key properties. The issue can be naming of many-to-many keys and junction tables.
ToTable is needed only if your entity does not follow naming convention or if you map some advance inheritance or splitting.
EF uses a lot of default conventions which drive how the names should be defined. Conventions can be removed.
You will not do anything wrong if you define EntityConfiguration for each table - it will at least allow you learning what is needed and your mapping will be explicit / self documented.