简体   繁体   English

如何更新具有多对多关系的实体框架4模型?

[英]Updating an entity framework 4 model with a many to many relationship, how?

I have a entity model with the following objects: 我有一个带有以下对象的实体模型:

  • House
  • Task 任务
  • TaskType 任务类型

I have the following relationships: House <1>----< > Task (One to many) Task < >----<1> TaskType (Many to one) 我具有以下关系:房子<1> ---- < >任务(一对多)任务< > ---- <1> TaskType(多对一)

Now, I wanted to add a many to many relationship between House and TaskType, to set which TaskTypes are available for a house. 现在,我想在House和TaskType之间添加多对多关系,以设置可用于房屋的TaskType。

What is the correct way to do this in Visual Studio 2010 without losing data in the database. 在Visual Studio 2010中执行此操作而不丢失数据库中数据的正确方法是什么。

If I do this on a brand new model, which doesn't have any database generated yet, it works fine, but if I try to add it after I've generated the database the first time, I will loose all my data since the genereated SQL drops all tables. 如果我是在尚未生成任何数据库的全新模型上执行此操作,则它可以正常工作,但是如果我在第一次生成数据库后尝试添加它,则将删除自通用SQL删除所有表。

If I try to create a table manually in the DB called HouseTaskTypes with two columns (House_Id and TaskType_Id) with foreign keys to House and TaskTypes, it looks weird when I update the model from the database. 如果我尝试在数据库中手动创建一个名为HouseTaskTypes的表,该表具有两列(House_Id和TaskType_Id),其中包含House和TaskTypes的外键,那么从数据库更新模型时,它看起来很奇怪。

I can probably get it to work with some manual adjustments, but I'd like to know what the correct way is of adding a many-to-many association/relationship in an already existing Entity Framework model. 我可能可以通过一些手动调整来使其工作,但是我想知道在现有的Entity Framework模型中添加多对多关联/关系的正确方法是什么。

All ideas are appreciated! 所有的想法表示赞赏!

Migrating an existing database schema to a new one is not supported in EF 4.1. EF 4.1不支持将现有数据库架构迁移到新架构。 From msdn : 来自msdn

Code First does not support migration of an existing database schema. Code First不支持现有数据库架构的迁移。 The Entity Framework 4.1 does support dropping and re-creating a database schema when the model changes through using database initializers. 当模型通过使用数据库初始化程序更改时,Entity Framework 4.1确实支持删除和重新创建数据库架构。 The following initializers are supported: CreateDatabaseIfNotExist, DropCreateDatabaseAlways, and DropCreateDatabaseIfModelChanges 支持以下初始化程序:CreateDatabaseIfNotExist,DropCreateDatabaseAlways和DropCreateDatabaseIfModelChanges

So, to solve your problem, I would 所以,要解决您的问题,我会

  • set the intializer to CreateDatabaseIfNotExist, or turn it off 将初始化器设置为CreateDatabaseIfNotExist,或将其关闭
  • manually add the table to the database 手动将表添加到数据库
  • manually add the navigation properties to your model 手动将导航属性添加到模型
  • manually map the many-to-many relationship (see below) 手动映射多对多关系(请参见下文)
  • then add any data to the new table either through your app or manually 然后通过您的应用或手动将任何数据添加到新表中

To manually map the relationship add the following method to your DBContext 要手动映射关系,请在您的DBContext中添加以下方法

 class protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
    base.OnModelCreating(modelBuilder);

    modelBuilder.Configurations.Add(new HouseConfiguration());   `
 }

Then, in the same, or reachable, namespace as your DBContext class, add a configuration class for one side of the many-to-many relationship. 然后,在与DBContext类相同或可访问的名称空间中,为多对多关系的一侧添加一个配置类。 The purpose of this class is do the actual mapping. 此类的目的是进行实际映射。 I typically have a separate namespace just for these configuration classes. 我通常为这些配置类有一个单独的命名空间。

class HouseConfiguration : EntityTypeConfiguration<House>
{
    public HouseConfiguration()
    {
        // many-to-many w/tasktypes
        this.HasMany(h => h.TaskTypes)
            .WithMany(tt => tt.Houses) 
            .Map(m =>
                {
                    m.ToTable("HouseTaskTypes");
                    m.MapLeftKey("HouseId");
                    m.MapRightKey("TaskTypeId");
                });

    }
}

You will have to double check the property names/key names, but this should do it. 您将必须仔细检查属性名称/键名称,但是应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM