C# EF代码第一个多对多BaseEntity继承

C# EF代码第一个多对多BaseEntity继承,c#,.net,entity-framework,C#,.net,Entity Framework,我希望所有继承BaseEntity的实体都有一个实体的集合,这是我的示例 public abstract class BaseEntity : IEntity { [Key] public Guid Id { get; set; } public virtual ICollection<Note> Notes { get; set; } protected BaseEntity() { Id = Guid.NewGuid()

我希望所有继承BaseEntity的实体都有一个实体的集合,这是我的示例

public abstract class BaseEntity : IEntity
{
    [Key]
    public Guid Id { get; set; }

    public virtual ICollection<Note> Notes { get; set; }

    protected BaseEntity()
    {
        Id = Guid.NewGuid();
        Notes = new List<Note>();
    }
}

public class Note
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }

    public Note()
    {
        Id = Guid.NewGuid();
    }
}

public class Student : BaseEntity
{
    public string Name { get; set; }
    public School School { get; set; }
    public string SomeInfo { get; set; }
}

public class School : BaseEntity
{
    public string Name { get; set; }
    public int Rating { get; set; }
    public int Size { get; set; }
}
公共抽象类BaseEntity:Entity
{
[关键]
公共Guid Id{get;set;}
公共虚拟ICollection注释{get;set;}
受保护的BaseEntity()
{
Id=Guid.NewGuid();
注释=新列表();
}
}
公开课堂讲稿
{
[关键]
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共说明()
{
Id=Guid.NewGuid();
}
}
公共班级学生:BaseEntity
{
公共字符串名称{get;set;}
公立学校{get;set;}
公共字符串SomeInfo{get;set;}
}
公立学校:BaseEntity
{
公共字符串名称{get;set;}
公共整数评级{get;set;}
公共整数大小{get;set;}
}
我想把它映射成这样,因为默认情况下,TableNotes具有所有表的所有Id(学生Id、学校Id等)


你知道怎么做,或者搜索什么吗?

你可以在notes表中添加ObjectId,指向相关对象(学生、学校等)。并用[ForeignKey(“ObjectId”)]属性装饰基类

public abstract class BaseEntity : IEntity
{
    [Key]
    public Guid Id { get; set; }

    [ForeignKey("ObjectId")]
    public virtual ICollection<Note> Notes { get; set; }

    protected BaseEntity()
    {
        Id = Guid.NewGuid();
        Notes = new List<Note>();
    }
}

public class Note
{
    [Key]
    public Guid Id { get; set; }
    public Guid ObjectId { get; set; } //student, school, etc
    public string Name { get; set; }
    public string Value { get; set; }

    public Note()
    {
        Id = Guid.NewGuid();
    }
}
公共抽象类BaseEntity:Entity
{
[关键]
公共Guid Id{get;set;}
[外键(“目标”)]
公共虚拟ICollection注释{get;set;}
受保护的BaseEntity()
{
Id=Guid.NewGuid();
注释=新列表();
}
}
公开课堂讲稿
{
[关键]
公共Guid Id{get;set;}
公共Guid ObjectId{get;set;}//学生、学校等
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共说明()
{
Id=Guid.NewGuid();
}
}

这是一对多关系,不需要映射表,因为您使用guid作为主键,不需要担心对象id在不同的表中重复。

您可以将ObjectId添加到notes表中,以指向相关对象(学生、学校等)。并用[ForeignKey(“ObjectId”)]属性装饰基类

public abstract class BaseEntity : IEntity
{
    [Key]
    public Guid Id { get; set; }

    [ForeignKey("ObjectId")]
    public virtual ICollection<Note> Notes { get; set; }

    protected BaseEntity()
    {
        Id = Guid.NewGuid();
        Notes = new List<Note>();
    }
}

public class Note
{
    [Key]
    public Guid Id { get; set; }
    public Guid ObjectId { get; set; } //student, school, etc
    public string Name { get; set; }
    public string Value { get; set; }

    public Note()
    {
        Id = Guid.NewGuid();
    }
}
公共抽象类BaseEntity:Entity
{
[关键]
公共Guid Id{get;set;}
[外键(“目标”)]
公共虚拟ICollection注释{get;set;}
受保护的BaseEntity()
{
Id=Guid.NewGuid();
注释=新列表();
}
}
公开课堂讲稿
{
[关键]
公共Guid Id{get;set;}
公共Guid ObjectId{get;set;}//学生、学校等
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共说明()
{
Id=Guid.NewGuid();
}
}

这是一对多关系,不需要映射表,因为您使用guid作为主键,不需要担心对象id在不同的表中重复。

您可以将ObjectId添加到notes表中,以指向相关对象(学生、学校等)。并用[ForeignKey(“ObjectId”)]属性装饰基类

public abstract class BaseEntity : IEntity
{
    [Key]
    public Guid Id { get; set; }

    [ForeignKey("ObjectId")]
    public virtual ICollection<Note> Notes { get; set; }

    protected BaseEntity()
    {
        Id = Guid.NewGuid();
        Notes = new List<Note>();
    }
}

public class Note
{
    [Key]
    public Guid Id { get; set; }
    public Guid ObjectId { get; set; } //student, school, etc
    public string Name { get; set; }
    public string Value { get; set; }

    public Note()
    {
        Id = Guid.NewGuid();
    }
}
公共抽象类BaseEntity:Entity
{
[关键]
公共Guid Id{get;set;}
[外键(“目标”)]
公共虚拟ICollection注释{get;set;}
受保护的BaseEntity()
{
Id=Guid.NewGuid();
注释=新列表();
}
}
公开课堂讲稿
{
[关键]
公共Guid Id{get;set;}
公共Guid ObjectId{get;set;}//学生、学校等
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共说明()
{
Id=Guid.NewGuid();
}
}

这是一对多关系,不需要映射表,因为您使用guid作为主键,不需要担心对象id在不同的表中重复。

您可以将ObjectId添加到notes表中,以指向相关对象(学生、学校等)。并用[ForeignKey(“ObjectId”)]属性装饰基类

public abstract class BaseEntity : IEntity
{
    [Key]
    public Guid Id { get; set; }

    [ForeignKey("ObjectId")]
    public virtual ICollection<Note> Notes { get; set; }

    protected BaseEntity()
    {
        Id = Guid.NewGuid();
        Notes = new List<Note>();
    }
}

public class Note
{
    [Key]
    public Guid Id { get; set; }
    public Guid ObjectId { get; set; } //student, school, etc
    public string Name { get; set; }
    public string Value { get; set; }

    public Note()
    {
        Id = Guid.NewGuid();
    }
}
公共抽象类BaseEntity:Entity
{
[关键]
公共Guid Id{get;set;}
[外键(“目标”)]
公共虚拟ICollection注释{get;set;}
受保护的BaseEntity()
{
Id=Guid.NewGuid();
注释=新列表();
}
}
公开课堂讲稿
{
[关键]
公共Guid Id{get;set;}
公共Guid ObjectId{get;set;}//学生、学校等
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共说明()
{
Id=Guid.NewGuid();
}
}


这是一对多关系,不需要映射表,因为您使用guid作为主键,您不需要担心对象id在不同的表中重复。

您的意思是您不想拥有
manytomanyotes
表/关系吗?我想拥有多对多表,但是我不知道如何映射它,所以所有表都会使用它。你的意思是你不想要
manytomanyotes
表/关系吗?我想要多对多表,但是我不知道如何映射它,所以所有表都会使用它。你的意思是你不想要
manytomanyotes
表/关系吗?我想要多对多表,但是我不知道如何映射它,所以所有表都会使用它。你的意思是你不想要
manytomanyotes
表/关系吗?我想要多对多表,但我不知道如何映射它,以便所有表都将使用itnope Doesn Not work Approvement\u Notes\u Source\u Approvement\u Notes\u Target::关系约束中从属角色和主体角色中的属性数必须相同。应该可以,请检查代码。在这种情况下,您的表只有一个Id字段吗?这是可行的,但是您知道如何创建这样一个没有外键但具有导航属性的表,因为没有外键时性能会非常慢key@AndrianDurlestean没有外键是什么意思,那你怎么知道没有外键呢