ef-code-first

How to customize foreign key column name using Entity Framework Code-first

百般思念 提交于 2019-12-18 16:11:50
问题 I have the following model public class Foo { public int Id { get; set; } public IList<Bar> Bars { get; set; } } public class Bar { public int Id { get; set; } } When I generate the database schema from this using EF Code first it creates the following tables: Foo Id (PK, int, not null) Bar Id (PK, int, not null) Foo_Id (FK, int null) Is it possible to change the name of the Foo_Id foreign key using attributes? EDIT: Here is the version that does what I need: public class Foo { public int Id

EF Code First: is good to call DetectChanges just before SaveChanges?

怎甘沉沦 提交于 2019-12-18 15:47:32
问题 I have read few articles (article1, article2) about Entity Framework that it calls DetectChanges many times, which makes it very slow when working with large amounts of data. Can i, for example, disable the autoDetectChanges when i initialize the context, and just call DetectChanges() before calling .SaveChanges() ? Will the context recognize the inserted/changed/deleted entities? var _dbContext = new ProjectContext(); _dbContext.Configuration.AutoDetectChangesEnabled = false; // add/edit

Entity splitting when key column has different names?

十年热恋 提交于 2019-12-18 14:56:14
问题 I'm using Entity Framework 4.3.1 Code-First and I need to split an entity between two tables. The tables have a primary key shared, and it is 1-to-1, but the columns are not named the same on each table. I don't control the data layout, nor can I request any changes. So for example, the SQL tables could be And this would be my entity... public class MyEntity { public int Id {get; set;} public string Name {get;set} public string FromAnotherTable {get;set;} } And here is the mapping I have.

Ambiguous references with the exact same namespace

亡梦爱人 提交于 2019-12-18 12:43:55
问题 The RC class is not linked to a database, it is a simple class. The class is at only one place and is not partial. The Aérochem.Domain dll project compiles just fine. Note: If I select one of the two identical namespaces in the quick fix menu, it does nothing. Note2: This happens to a couple of classes (some related to a database, some not) Is there a fix to that or a way to figure what's wrong? 回答1: I had the same problem. I use ReSharper. The solution to my problem was not cleaning the

Get SQL file for specific migration in Entity Framework 6 C#

白昼怎懂夜的黑 提交于 2019-12-18 12:29:51
问题 In Entity Framework 6, I need to get the SQL script for specific migration file and I already update the database. I found that in update-database command I can add script parameter to export it to SQL but I already updated the database. I found in Entity Framework Core, a command called Script-Migration with script name as an argument. Is there any similar command in Entity framework? i also i tried the below, and i got empty SQL file Update database command 回答1: Yes, you could do like

EF Code First Migrations to Deploy Older Version

会有一股神秘感。 提交于 2019-12-18 12:22:18
问题 I'm using TFS Release Management to do continuous integration and deployment. I'm using migrate.exe to perform the database migration during deployment, and this works great when you're going from an older version to a newer version. When you want to deploy an older version of the application, however, it gets more muddy. Basically, the assembly that holds your migrations for a context must know how to go from say version 3 to version 2. Normally, you use the assemblies you're about to deploy

Entity Framework 4: Code First - Creating db in another schema? MapSingleType?

久未见 提交于 2019-12-18 12:16:08
问题 I have a database and i using 2 different schemas. Schemas are like namespaces (correct me if i am wrong). This way i have 1 db and currently 2 schemas... so the tables in 1 schema can be named the same as the tables in the other schema because they are in separate schemas. How do i get EF Code first to talk to a different schema and not the default schema? Is it somethign to do with MapSingleType and overriding a method or can i do something else? ANy help really appreciated. 回答1: You can

How to use Membership provider with EF Code First?

て烟熏妆下的殇ゞ 提交于 2019-12-18 12:13:07
问题 I have models based on EF Code First and I want to use them with the default MembershipProvider, but I don't know how to write the model correctly, so it won't erase all my data on recreating the tables when there were changes made to the model. 回答1: Currently (EF 4.1 CTP) EF Code First doesn't have that option. It always drops a table if you made changes to model. Update: EF 4.1 RTM allows you to create a custom database initializer and specify creation of db objects and data seeding. 回答2:

Create a DbSet<T> dynamically in Entity Framework?

*爱你&永不变心* 提交于 2019-12-18 12:09:51
问题 In LINQ to SQL, I can create a repository dynamically using DataContext.GetTable<T> . Is there a similar way to do this in Entity Framework 4 other than declaring the properties on the specific DbContext ? For example: public MyDbContext: DbContext { public DbSet<MySet> MySets {get; set;} } I would like to know how can I create/get a reference to MySets dynamically as I can with LINQ to SQL as in: var mySet = MyDbContext.GetTable<MySet>(); 回答1: DbContext has method for this: var set = context

How do you actually perform relationships in Entity Framework 4 Code-First CTP 5?

不问归期 提交于 2019-12-18 11:35:21
问题 I would like to have a short example on how do you actually perform relationships in Entity Framework 4 Code-First CTP 5 ? Would love an example for these kind of relations : * one-to-many * many-to-many Thanks a lots! 回答1: One to One public class One { public int Id {get;set;} public virtual Two RelationTwo {get;set;} } public class Two { public int Id {get;set;} public virtual One RelationOne {get;set;} } Things to note, it has to be virtual One to Many public class One { public int Id {get