C# 一个表属性与两个表属性相关.NET MVC

C# 一个表属性与两个表属性相关.NET MVC,c#,asp.net,sql-server,asp.net-mvc,entity-framework,C#,Asp.net,Sql Server,Asp.net Mvc,Entity Framework,我有一个名为News的表,我想存储高质量和低质量的图像以及每个新图像的信息,因此,我创建了另一个名为NewsAttachment的表,该表应该与News相关,但我已经创建了另一个名为NewsComment的表,该表也与News相关,当我想将两者定义为外键时,SQL Server在新闻和新闻附件中添加另一列 这是新闻课程: public class News { [Key] public int NewsId { get; set; } //primary key .

我有一个名为News的表,我想存储高质量和低质量的图像以及每个新图像的信息,因此,我创建了另一个名为NewsAttachment的表,该表应该与News相关,但我已经创建了另一个名为NewsComment的表,该表也与News相关,当我想将两者定义为外键时,SQL Server在新闻新闻附件中添加另一列


这是新闻课程:

public class News
{
    [Key]
    public int NewsId { get; set; } //primary key
    .
    .
    .
    //navigation property
    public virtual List<Comments> Comments { get; set; }
    public virtual List<NewsAttachments> Comments { get; set; }
    public News()
    {

    }
}
public class Comments
{
    [Key]
    public int CommentId { get; set; }

    [Required]
    public int NewsId { get; set; }  //Foreign key of News
    .
    .
    .
    //navigation property
    public virtual News News { get; set; }
    public Comments()
    {

    }
}
public class NewsAttachment
{
    [Key]
    public int NewsAttachmentId { get; set; }

    [Required]
    public int NewsId { get; set; }  //Foreign key of News


    //Image information property
    public string FileName { get; set; }
    public string RealFileName { get; set; }
    public string FilePath { get; set; }
    public string FilePageType { get; set; }

    //navigation property
    public News News { get; set; }
    public NewsAttachment()
    {

    }
}

这是新闻附件课程:

public class News
{
    [Key]
    public int NewsId { get; set; } //primary key
    .
    .
    .
    //navigation property
    public virtual List<Comments> Comments { get; set; }
    public virtual List<NewsAttachments> Comments { get; set; }
    public News()
    {

    }
}
public class Comments
{
    [Key]
    public int CommentId { get; set; }

    [Required]
    public int NewsId { get; set; }  //Foreign key of News
    .
    .
    .
    //navigation property
    public virtual News News { get; set; }
    public Comments()
    {

    }
}
public class NewsAttachment
{
    [Key]
    public int NewsAttachmentId { get; set; }

    [Required]
    public int NewsId { get; set; }  //Foreign key of News


    //Image information property
    public string FileName { get; set; }
    public string RealFileName { get; set; }
    public string FilePath { get; set; }
    public string FilePageType { get; set; }

    //navigation property
    public News News { get; set; }
    public NewsAttachment()
    {

    }
}

我的问题摘要: 我希望News中的NewsId与表中的其他两个属性有关系,但它遇到了问题


如果无法解决此问题,请为我提供另一种方法,将我的两张照片及其信息保存在我的数据库中。

您必须向EF明确声明,以便EF知道哪个是外键:

public class NewsAttachment
{
    [Key]
    public int NewsAttachmentId { get; set; }

    public int NewsId { get; set; }  //Foreign key of News    

    //Image information property
    public string FileName { get; set; }
    public string RealFileName { get; set; }
    public string FilePath { get; set; }
    public string FilePageType { get; set; }

    [ForeignKey("NewsId")]
    public News News { get; set; }
    public NewsAttachment()
    {

    }
}

您必须专门向EF声明它,以便它知道哪个是外键:

public class NewsAttachment
{
    [Key]
    public int NewsAttachmentId { get; set; }

    public int NewsId { get; set; }  //Foreign key of News    

    //Image information property
    public string FileName { get; set; }
    public string RealFileName { get; set; }
    public string FilePath { get; set; }
    public string FilePageType { get; set; }

    [ForeignKey("NewsId")]
    public News News { get; set; }
    public NewsAttachment()
    {

    }
}

如果任何新闻都有几个unicue附件,我看不出有任何问题。如果相同的附件可以用于不同的新闻?如果任何新闻有多个unicue附件,我看不出有任何问题。如果相同的附件可以用于不同的新闻?