When I generated the model of the database using ADO.NET Entity Data Model, the generated model had ugly foreign keys names. I tried to change the names in SQL Server Management Studio and then I generated the model again but it did not adopt the new names of the foreign keys.
So please could anyone help me to solve this problem?
With automatic migration the name is assigned by EF.
With manual migration you can specify the name in AddForeignKey method. Here the specifications
https://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigration.addforeignkey(v=vs.113).aspx
EDIT
During automatic migration the EF build the name with this logic
FK_<sourceTable>_<targetTable>_<sourceFieldsList>
See EF framework source code ForeignKeyOperation.cs property DefaultName
Related
I am using EF core 2.0 and using scaffolding reverse engineering database first to generate models.
some of tables does not have any primary key and EF Unable to generate entity type for table.
Any advice how can I do this other then adding manually.
So far either Entity Framework or Entity Framework Core does not support generating table from EF/EF Core model class without primary key.
But this is a requested feature in Entity Framework Core. Here is the details about it:
Allow code generation for tables without a primary key
You could upgrade to EF core 3.0 which is currently in preview, that is now possible.
I'm using an existing database and I have mapped one of the tables as an entity (as i needed to map a foreign key).
So when it comes to initialising this database I would like EF to ignore this entity since it already exists.
How would I go about doing this?
You should create an empty migration as the first migration with the -IgnoreChanges flag to allow Entity Framework to get a snapshot of the existing database.
So out of the gate use:
Add-Migration InitialMigration -IgnoreChanges
and that will create a blank migration but it will update the Entity Framework metadata allowing the existing tables to exist and not be touched by migrations.
Also to be mentioned that the naming conventions that Entity Framework expects and your database schema may differ. You may need to manually setup the foreign keys using the Fluent API.
I didnt check with EF 6 specifically, but I think default EF behavious is that when the database exists, then it presume all model be ready and therefore will create no tables. If you want your initialization code to create tables with code first, use initialization code for prepare data. Look here :
http://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx
I'm using EF5.
Does a visual studio template exists which can generate POCOs having appropriate attributes applied to them from the DataAnnotations namespace (like Key and Foreign Key)
I can generate POCOs using the EF 5.x DBContext Generator which utilizes an existing Entity Data Model, but then I have to manually go setup the Keys and Foreign keys.
For a larger data model this is a pain!
This worked perfectly for me - http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
I have VS 2012 and the existing database (sql 2012).
In the database, there are one to one relationships and some columns are non-nullable with default value like " ". EDM generated the edmx diagram, but all the relationships are one to many.
I have Customer table with column AddressReference1 as not nullable and its default value is " ". After generated entity class Customer, I deleted this property in .edmx designer, then I compile the project.
If I delete one of non-nullable property with default value of " " from the designer, the error was generated as following when I compiled the project.
Error 1 Error 3023: Problem in mapping fragments starting at line
568:Column Customer.AddressReference1 in table Customer must be
mapped: It has no default value and is not nullable.
C:\Users\cliu\Documents\Visual Studio
2012\Projects\FulfillmentService\ShipmentModel.edmx 569 15 FulfillmentService
How do I solve this problem if I want to remove some properties from the entity class?
If you want to delete some properties from your model, you should delete them in your database to. Otherwise, EF conventions can't do their jobs correctly.
Usually, when you want full control over your model and its mappings, you should use Code-First approach.
Database-First approach depends heavily on EF conventions which dictate some limitations to developer because of their abstractions - Conventions!.
Although it is possible to change the db-first approach to code-first and remove some of those conventions by overriding OnModelCreating event, however this will cause to some strange problems which force you double-work only to fix them...!
In your current situation, you have no choice but accept one-to-many relations and keeping your model sync with your database.
Everything I find through Google refers to Code First so I'm wondering what the Key attribute is actually doing in regards to a Database First design? I'm mainly curious because a lot of the entities contain composite keys so I've been adding the Key annotation to the respective properties, but is this really necessary? And if so, what do I gain from it?
If you're using the database-first workflow (i.e. you have an .edmx file in your solution). Then the Key attribute will have no effect.
However, if you're using the code-first workflow to map to an existing database, the Key attribute tells code first that that property is part of the entity's primary key.
For more info on the different workflows see this video: Visual Studio Toolbox: Entity Framework Part 1