我怎么名称EF6一个多对多的表,我需要添加特殊的映射呢? [英] How do I name a many-many table for EF6 and do I need to add special mapping for this?

查看:166
本文介绍了我怎么名称EF6一个多对多的表,我需要添加特殊的映射呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF 6和试图映射一个多对多的关系。到目前为止,我有:

 公共部分类ObjectiveDetail
{
    公共ObjectiveDetail()
    {
        this.SubTopics =新的List<&子话题GT;();
    }
    公众诠释ObjectiveDetailId {搞定;组; }
    公共字符串文本{搞定;组; }
    公共虚拟的ICollection<&子话题GT;子专题{搞定;组; }
}公共部分类子话题
{
    公共子话题()
    {
        this.ObjectiveDetails =新的List< ObjectiveDetail>();
    }
    公众诠释SubTopicId {搞定;组; }
    公众诠释号码{搞定;组; }
    公共字符串名称{;组; }
    公共虚拟的ICollection< ObjectiveDetail> ObjectiveDetails {搞定;组; }
}

我们的DBA将会在code为众多写了许多表。如果这是如下
与表名 ObjectiveDetailSubTopic 或完全不同的东西?

  CREATE TABLE [DBO]。[ObjectiveDetailSubTopic](
    [ObjectiveDetailId] INT NOT NULL,
    [SubTopicId] INT NOT NULL
);

谁能告诉我,如果这是创建该表的正确方法。另外我必须
添加一些code到ObjectiveDetail和子话题类映射到新加入的类等等
EF会知道该怎么办?


解决方案

  

我们的DBA将会在code为众多写了许多表。应该
  这与ObjectiveDetailSubTopic或表名称如下
  完全不同的东西?


只要你遵循SQL数据库表的命名约定,表名可以是任何东西。我通常命名连接表像你这样的,通过连接这两个表名称。

要使用SQL创建连接表,见下图:

  CREATE TABLE [DBO]。[ObjectiveDetailSubTopic](
        ObjectiveDetailSubTopicId INT标识的主键,
        ObjectiveDetailId INT NOT NULL,
        SubTopicId INT NOT NULL,
        外键(ObjectiveDetailId)引用ObjectiveDetail(ObjectiveDetailId)
        外键(SubTopicId)引用子话题(SubTopicId)
    );

但你并不需要通过自己创建的连接表,实体框架会为您创建它。你只需要映射与流利的API的关系,你的DbContext类象下面这样:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
    {
        modelBuilder.Entity< ObjectiveDetail>()。
            的hasMany(C => c.SubTopics)。
            WithMany(P => p.ObjectiveDetails)。
            地图(M = GT;
                    {
                        m.MapLeftKey(ObjectiveDetailId);
                        m.MapRightKey(SubTopicId);
                        m.ToTable(ObjectiveDetailSubTopic);
                    });
    }

I am using EF 6 and trying to map a many to many relationship. So far I have:

public partial class ObjectiveDetail
{
    public ObjectiveDetail()
    {
        this.SubTopics = new List<SubTopic>();
    }
    public int ObjectiveDetailId { get; set; }
    public string Text { get; set; }
    public virtual ICollection<SubTopic> SubTopics { get; set; }
}

public partial class SubTopic
{
    public SubTopic()
    {
        this.ObjectiveDetails = new List<ObjectiveDetail>();
    }
    public int SubTopicId { get; set; }
    public int Number { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }
}

Our DBA is going to write the code for the many to many table. Should this be as follows with a table name of ObjectiveDetailSubTopic or something completely different ?

CREATE TABLE [dbo].[ObjectiveDetailSubTopic] (
    [ObjectiveDetailId] INT NOT NULL,
    [SubTopicId]        INT NOT NULL
);

Can someone tell me if this is the correct way to create the table. Also do I have to add some code to map the ObjectiveDetail and SubTopic classes to the new join class so EF will know what to do?

解决方案

Our DBA is going to write the code for the many to many table. Should this be as follows with a table name of ObjectiveDetailSubTopic or something completely different ?

As long as you follow the SQL Database table naming conventions, the table name can be anything. I usually name the join table like yours, by connecting the two table names.

To create the join table using sql, see below:

 CREATE TABLE [dbo].[ObjectiveDetailSubTopic](
        ObjectiveDetailSubTopicId int identity primary key,
        ObjectiveDetailId INT NOT NULL,
        SubTopicId  INT NOT NULL,
        foreign key(ObjectiveDetailId) references ObjectiveDetail(ObjectiveDetailId ),
        foreign key(SubTopicId) references SubTopic(SubTopicId )
    );

But you don't need to create the join table by your own, Entity Framework will create it for you. You just need to mapping the relationship with the Fluent API in your DbContext class like below:

   protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ObjectiveDetail>().
            HasMany(c => c.SubTopics).
            WithMany(p => p.ObjectiveDetails).
            Map(m =>
                    {
                        m.MapLeftKey("ObjectiveDetailId ");
                        m.MapRightKey("SubTopicId ");
                        m.ToTable("ObjectiveDetailSubTopic");
                    });
    }

这篇关于我怎么名称EF6一个多对多的表,我需要添加特殊的映射呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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