Entity Framework and Models with Simple Arrays

后端 未结 1 787
故里飘歌
故里飘歌 2021-01-20 08:05

This is my model class.

public class Lead
{
    private readonly ObservableCollection m_tags = new ObservableCollection();

    p         


        
1条回答
  •  迷失自我
    2021-01-20 08:51

    Since your model has to be represented in a relational way, you can only use primitive types (that have an equivalent in a SQL DB) or other entities within a entity definition - that means the tags are represented by their own entity. In your case it would be something like this using Code first approach:

    public class Lead
    {
        public int LeadId { get; set; }
        public string Name { get; set; }
        public virtual ICollection Tags { get; set; }
    }
    
    public class Tag
    {
        public int TagId { get; set; }
        public string Name { get; set; }
    }
    
    public class SomeContext : DbContext
    {
        public DbSet Leads { get; set; }
        public DbSet Tags { get; set; }
    }
    

    This (by default) will be represented in the database as a table Leads, a table Tags, and a relationship table LeadTags that only contains {LeadId, TagId} pairs.

    0 讨论(0)
提交回复
热议问题