code-first

Entity Framework Code First and Multiple Assemblies

狂风中的少年 提交于 2019-12-18 17:00:10
问题 I have a subclass in a different assembly to its base class. The parent is a POCO class used for EF Code First. When I try to add an instance of inherited class to the database I get InvalidOperationException: "Object mapping could not be found for Type with identity 'Foo.Bar.MyInheritedClass'". It works fine if subclass is in same assembly as base class. In regular EF the solution seems to be a call to ObjectContext.MetadataWorkspace.LoadFromAssembly(assembly) . But I can't figure out how

Entity Framework Seed method is not being called

故事扮演 提交于 2019-12-18 15:44:49
问题 We are using Entity Framework 4.4 and using migrations. The database already exists and we need to update it on regular basis. The seed method, however, is not being called and so lookup values are not being added. The code looks as follow: internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext> { public Configuration() { AutomaticMigrationsEnabled = false; SetSqlGenerator("System.Data.SqlClient", new OurSqlServerMigrationSqlGenerator()); } protected override void Seed

Can I access the discriminator value in TPH mapping with Entity Framework 4 CTP5

怎甘沉沦 提交于 2019-12-18 14:54:56
问题 Using Entity Framework 4 CTP5 Code First and this example Is it possible to access the discriminator value? I would like to use it in a projection like context.BillingDetails.Select(x => new { Number = x.Number, DiscrimitatorValue = /* how do I get the discriminator value? */ }); From this post I understand the discriminator cannot be mapped to a property but is there any other way of accessing it? 回答1: I may be late to the game on this one, but I just added a getter property to the base

ALTER TABLE DROP COLUMN failed because one or more objects access this column

不羁的心 提交于 2019-12-18 13:50:55
问题 I am trying to do this: ALTER TABLE CompanyTransactions DROP COLUMN Created But I get this: Msg 5074, Level 16, State 1, Line 2 The object 'DF__CompanyTr__Creat__0CDAE408' is dependent on column 'Created'. Msg 4922, Level 16, State 9, Line 2 ALTER TABLE DROP COLUMN Created failed because one or more objects access this column. This is a code first table. Somehow the migrations have become all messed up and I am trying to manually roll back some changed. I have no idea what this is: DF_

Using EF4 Code First: How can I change the Model without losing data

我只是一个虾纸丫 提交于 2019-12-18 12:29:49
问题 In my Global.asax I have the following line: Database.SetInitializer<myDbSupport> (new DropCreateDatabaseIfModelChanges<myDbSupport>()); If I don't have this, then when i change the model, the sdf database will not have the correct structure. This is ok in a dev environment, but when I move to production and want a DB structure update, i of course, can't afford to drop the table, and lose the data. Is it possible to script DB changes, and run this update before deploying the model with the

Many-To-Many Relationship in Code-First EF4

时光毁灭记忆、已成空白 提交于 2019-12-18 11:56:38
问题 How do you represent a many-to-many relationship in the EF4 Code-First CTP3? For example if I have the following classes: class User { public int Id { get; set; } public string Name { get; set; } public ICollection<Profile> Profiles { get; set; } } class Profile { public int Id { get; set; } public string Name { get; set; } } In the database there is a UserProfiles table that has the FK for User and FK for Profile. How can I map this? EDIT: I understand how to to currently map with having a

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

EF Code First Migration with Multiple Database / DbContext

三世轮回 提交于 2019-12-18 11:33:13
问题 I have two database each with their own dbcontext. I've setup two migration configurations. I can add a migration for the first db ust fine ( Add-Migration DB1_InitialCreate -ConfigurationTypeName DB1Configuration ). When I try to create an initial migration with the second db using: Add-Migration DB2_InitialCreate -ConfigurationTypeName DB2Configuration , I get the following error: Unable to generate an explicit migration because the following explicit migrations are pending:

EF code first: How to delete a row from an entity's Collection while following DDD?

本秂侑毒 提交于 2019-12-18 10:57:05
问题 So here's the scenario: DDD states that you use a repository to get the aggregate root, then use that to add/remove to any collections it has. Adding is simple, you simple call .Add(Item item) on the Collection you wish to add to. A new row is added to the database when you save. However, deleting is different - calling .Remove(Item item) doesn't remove the item from the database, it simply removes the foreign key. So while, yes, it is technically no longer part of the collection anymore, it

Code First Fluent API and Navigation Properties in a Join Table

余生长醉 提交于 2019-12-18 04:54:17
问题 I have four entities that I would like to translate into database tables via code first fluent api (I'm using a model found at databaseanswers.org), but I'm not certain as to how. The problem I'm having is that SuggestedMenuId is being migrated across two different tables in a Composite key (MenuCourse and CourseRecipeChoice). Here's the message I'm getting: "One or more validation errors were detected during model generation: \tSystem.Data.Entity.Edm.EdmAssociationConstraint: : The number of