code-first

How can I use TPT inheritance models when primary keys have different names?

久未见 提交于 2019-11-30 16:25:24
Using Entity Framework 4.1 against a legacy database, I'm unable to generate a working set of TPT inheritance models that aren't pluralized and use different names for a common primary key. I am using database tables Organization, Account, and Company as illustrated below: Organization OrganizationID (int PK) OrgName (varchar) Company CompanyID (int PK) CompanyNo (varchar) Account AccountID (int PK) AccountNo (varchar) Account.AccountID and Company.CompanyID have a FK constraint that values in those columns must also be contained in Organization.OrganizationID so neither can exist without a

Entity Framework Code First and Multiple Assemblies

天大地大妈咪最大 提交于 2019-11-30 15:19:50
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 this relates to Code First. Any advice? I'm using Entity Framework 4.1 RC. I solved this by inheriting

Entity Framework CTP5 - How to Call Stored Procedure?

空扰寡人 提交于 2019-11-30 15:00:52
问题 This may be a simple answer, but i can't see how to execute a stored procedure with EF CTP5. In Entity Framework 4.0, we did this: ExecuteFunction("ContainerName.StoredProcName", new ObjectParameter("Id", id)) . Which is a method on the ObjectContext . But DbContext has no such method. How do we call a stored proc? Is it not supported in EF CTP5? EDIT: I found this thread, which states you need to do this: var people = context.People.SqlQuery("EXECUTE [dbo].[GetAllPeople]"); This raises some

EF CodeFirst create non-clustered primary key index

佐手、 提交于 2019-11-30 15:00:51
问题 I'm using EF 4.1 CodeFirst to create my DB. It seems that EF is creating all primary keys with clustered index, which is not optimal for us in one case(possibly more cases). Is there a way to tell EF to generate this table with primary key as a non-clustered index ? Of course we could do it manually using custom script after database is already created, but we have a lot of foreign keys pointing to this table, it would be quite a non-optimal solution if there is a better, more straight

Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First

旧时模样 提交于 2019-11-30 14:32:38
问题 How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs? At present, I have: public class Status { [Required] public int StatusId { get; set; } [Required] [DisplayName("Status")] public string Name { get; set; } } public class Restuarant { public int RestaurantId { get; set; } [Required] public string Name { get; set; } [Required] [EmailAddress] public string Email { get; set; } [Required] public string Telephone { get; set; } [Required] public int StatusId { get; set;

EF CodeFirst create non-clustered primary key index

别来无恙 提交于 2019-11-30 13:37:14
I'm using EF 4.1 CodeFirst to create my DB. It seems that EF is creating all primary keys with clustered index, which is not optimal for us in one case(possibly more cases). Is there a way to tell EF to generate this table with primary key as a non-clustered index ? Of course we could do it manually using custom script after database is already created, but we have a lot of foreign keys pointing to this table, it would be quite a non-optimal solution if there is a better, more straight forward way to do the same already during DB creation. Any help appreciated Ladislav Mrnka No there is no way

Entity Framework CTP5 - How to Call Stored Procedure?

删除回忆录丶 提交于 2019-11-30 13:13:24
This may be a simple answer, but i can't see how to execute a stored procedure with EF CTP5. In Entity Framework 4.0, we did this: ExecuteFunction("ContainerName.StoredProcName", new ObjectParameter("Id", id)) . Which is a method on the ObjectContext . But DbContext has no such method. How do we call a stored proc? Is it not supported in EF CTP5? EDIT: I found this thread , which states you need to do this: var people = context.People.SqlQuery("EXECUTE [dbo].[GetAllPeople]"); This raises some concerns: 1) You are now calling a stored prodedure on the set , not the context . Stored procedures

EntityFramework CodeFirst: CASCADE DELETE for same table many-to-many relationship

☆樱花仙子☆ 提交于 2019-11-30 12:41:21
I have an entry removal problem with the EntityFramework and a many-to-many relationship for the same entity. Consider this simple example: Entity: public class UserEntity { // ... public virtual Collection<UserEntity> Friends { get; set; } } Fluent API Configuration: modelBuilder.Entity<UserEntity>() .HasMany(u => u.Friends) .WithMany() .Map(m => { m.MapLeftKey("UserId"); m.MapRightKey("FriendId"); m.ToTable("FriendshipRelation"); }); Am I correct, that it is not possible to define the Cascade Delete in Fluent API? What is the best way to delete a UserEntity , for instance Foo ? It looks for

Creating BiDirectional One - One relationship in Entity Framework 4.1 Code First

你离开我真会死。 提交于 2019-11-30 12:33:40
问题 I want to created Bi-Directional One-One relationship between two entities using EF Code First. I have trouble with the following code. What do you think I should do? public class User { public string ID { get; set; } public string LastName { get; set; } public string Password { get; set; } public string FirstName { get; set; } public int ProfileID { get; set; } public Profile Profile { get; set; } } public class Profile { public int UserID { get; set; } public User User { get; set; } public

Entity Framework Seed method is not being called

限于喜欢 提交于 2019-11-30 12:06:48
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(KinectionDbContext context) { SeedLookupTables(context); } private static void SeedLookupTables