EF代码首先从数据库0..1到很多关系 [英] EF code first from database 0..1 to many relationship

查看:84
本文介绍了EF代码首先从数据库0..1到很多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从现有的数据库中生成一个实体框架代码第一个模型(而不改变数据库模式)。这个数据库过去已经被用于生成edmx模型,而我正在尝试使用Fluent Api或数据注释来实现等效的模型。



我已经无法使用复制是使用连接表(不是可空的外键)是0..1到许多。



所以它看起来像这样:

  TableA 
{
ID(PrimaryKey)
TableB(0或1)
}

JoinTable
{
TableA_FK(PrimaryKey,ForeignKey),
TableB_FK(ForeignKey)
}

TableB
{
ID(PrimaryKey)
表(许多)
}

这是可以在代码第一个样式中实现,或者我必须生成一个edmx模型,以便在EF中使用此数据库,而无需更改其模式?



非常感谢,
Phil

解决方案

这是一个没有使用JoinTable类的例子。连接表通过流畅的api配置。

  class DataContext:DbContext 
{
public DataContext string connectionString)
:base(connectionString)
{}

public DbSet< TableA> TableA {get;组; }
public DbSet< TableB> TableB {get;组;

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

modelBuilder.Entity< TableA>()。ToTable(TableA);
modelBuilder.Entity< TableB>()。ToTable(TableB);

modelBuilder.Entity< TableB>()
.HasMany(x => x.TableAs)
.WithMany()
.Map(m =
{
m.ToTable(JoinTable);
m.MapLeftKey(TableA_FK);
m.MapRightKey(TableB_FK);
}) ;
}
}

class TableA
{
public int ID {get;组; }
public TableB TableB {get;组; }
}

class TableB
{
public int ID {get;组; }
public ICollection< TableA> TableAs {get;组; }
}

这将生成以下迁移脚本,看起来像你拥有的模式。

  public override void Up()
{
CreateTable(
dbo.TableA
c => new
{
ID = c.Int(nullable:false,identity:true),
TableB_ID = c.Int(),
})
.PrimaryKey(t => t.ID)
.ForeignKey(dbo.TableB,t => t.TableB_ID)
.Index(t => t .TableB_ID);

CreateTable(
dbo.TableB,
c => new
{
ID = c.Int(nullable:false,identity:true ),
})
.PrimaryKey(t => t.ID);

CreateTable(
dbo.JoinTable,
c => new
{
TableA_FK = c.Int(nullable:false),
TableB_FK = c.Int(nullable:false),
})
.PrimaryKey(t => new {t.TableA_FK,t.TableB_FK})
.ForeignKey(dbo .TableB,t => t.TableA_FK,cascadeDelete:true)
.ForeignKey(dbo.TableA,t => t.TableB_FK,cascadeDelete:true)
.Index(t = > t.TableA_FK)
.Index(t => t.TableB_FK);

}


I am trying to generated an entity framework code first model from an existing database (without changing the database schema). This database has been used in the past to generate edmx models and I am trying to achieve the equivalent model using Fluent Api or data annotations.

The relationship I have been unable to reproduce is 0..1 to many using a join table (not a nullable foreign key).

So it would look something like this:

TableA
{
   ID (PrimaryKey)
   TableB (0 or 1)
}

JoinTable
{
   TableA_FK (PrimaryKey, ForeignKey),
   TableB_FK (ForeignKey)
}

TableB
{
   ID (PrimaryKey)
   TableAs (Many)
}

Is this achievable in the code first style or will I have to generate an edmx model in order to use this database in EF without changing its schema?

Many thanks, Phil

解决方案

Here is an example without using a JoinTable class. The join table is configured through the fluent api.

class DataContext : DbContext
    {
        public DataContext(string connectionString)
            : base(connectionString)
        { }

        public DbSet<TableA> TableA { get; set; }
        public DbSet<TableB> TableB { get; set; }

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

            modelBuilder.Entity<TableA>().ToTable("TableA");
            modelBuilder.Entity<TableB>().ToTable("TableB");

            modelBuilder.Entity<TableB>()
                .HasMany(x => x.TableAs)
                .WithMany()
                .Map(m =>
                {
                    m.ToTable("JoinTable");
                    m.MapLeftKey("TableA_FK");
                    m.MapRightKey("TableB_FK");
                });
        }
    }

    class TableA
    {
        public int ID { get; set; }
        public TableB TableB { get; set; }
    }

    class TableB
    {
        public int ID { get; set; }
        public ICollection<TableA> TableAs { get; set; }
    }

This will generate the following migration script, which looks like the schema you have.

public override void Up()
{
    CreateTable(
        "dbo.TableA",
        c => new
            {
                ID = c.Int(nullable: false, identity: true),
                TableB_ID = c.Int(),
            })
        .PrimaryKey(t => t.ID)
        .ForeignKey("dbo.TableB", t => t.TableB_ID)
        .Index(t => t.TableB_ID);

    CreateTable(
        "dbo.TableB",
        c => new
            {
                ID = c.Int(nullable: false, identity: true),
            })
        .PrimaryKey(t => t.ID);

    CreateTable(
        "dbo.JoinTable",
        c => new
            {
                TableA_FK = c.Int(nullable: false),
                TableB_FK = c.Int(nullable: false),
            })
        .PrimaryKey(t => new { t.TableA_FK, t.TableB_FK })
        .ForeignKey("dbo.TableB", t => t.TableA_FK, cascadeDelete: true)
        .ForeignKey("dbo.TableA", t => t.TableB_FK, cascadeDelete: true)
        .Index(t => t.TableA_FK)
        .Index(t => t.TableB_FK);

}

这篇关于EF代码首先从数据库0..1到很多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆