How can I name a lot a lot for EF6 and do I need to add a special mapping for this?

I use EF 6 and try to match many, many relationships. So far, I:

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 database administrator will write code for many tables. Should it be next with a table name ObjectiveDetailSubTopicor 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 a table. Also I need to add some code to map the ObjectiveDetail and SubTopic classes to the new join class so that EF will know what to do?

+1
source share
1 answer

. ObjectiveDetailSubTopic - ?

SQL, . , , .

sql, . :

 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 )
    );

, Entity Framework . Fluent API DbContext, :

   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");
                    });
    }
+2

All Articles