我奋力开创实体框架的关系三个表之间.. 我使用C#开发WPF应用程序,SQLite的3.0和Entity Framework 6的EntityFramework 1至0,1或一对多的关系
我有以下表(类):
- 投资者
- 投资
- ImageStore
我想创建一个模型,以便投资者和投资人(以及其他未来的其他类)可以存储0,1或多个图像..(因为从类名显而易见,投资者只能拥有一个配置文件图像并且投资类可以拥有多张图片) 我ImageStore类看起来是这样的:
public class ImageStore : PropertyChangedNotification
{
[Key]
public int ImageStoreId { get; set; }
[Required]
public string ImageFile { get; set; }
[Required]
public Byte[] ImageBlob { get; set; }
public string FileName { get; set; }
[Required]
public int FileSize { get; set; }
//public virtual ImageData ImageData { get; set; }
}
为了创建1至0,1或一对多的关系,我创建了另一个名为中间表:的ImageData,如下图所示(我不知道知道它是否真的是一个很好的做法,但我现在能想到的只有..)
public class ImageData : PropertyChangedNotification
{
[Key]
public int ImageDataId { get; set; }
[ForeignKey("Investment")]
public long? InvestmentId { get; set; }
[ForeignKey("Investor")]
public long? InvestorId { get; set; }
[ForeignKey("ImageStore")]
public int ImageStoreId { get; set; }
public virtual ImageStore ImageStore { get; set; }
public virtual Investment Investment { get; set; }
public virtual Investor Investor { get; set; }
}
我的投资类看起来是这样的:
public class Investor : PropertyChangedNotification
{
[Key]
public long InvestorId { get; set; }
[NotMapped]
[ForeignKey("ImageData")]
public List<int> ImageDataList { get; set; }
public virtual ICollection<ImageData> ImageDataCollection { get; set; }
public virtual ICollection<Investment> Investments { get; set; }
}
我的投资类如下所示:
public class Investment : PropertyChangedNotification
{
[Key]
public long InvestmentId { get; set; }
[ForeignKey("Investor")]
[Required]
public long FirstInvestorId { get; set; }
[NotMapped]
[ForeignKey("ImageData")]
public List<int> ImageDataList { get; set; }
public virtual ICollection<ImageData> ImageDataCollection { get; set; }
public virtual Investor Investor { get; set; }
[NotMapped]
[Required (ErrorMessage = "First Investor is Required")]
public Investor FirstInvestor { get; set; }
}
这是我与流畅的配置:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// MyData Database does not pluralize table names
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Investor>().HasOptional(s => s.ImageDataCollection);
modelBuilder.Entity<Investment>().HasOptional(s => s.ImageDataCollection);
//modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
当我开始调试应用程序,我收到以下错误:
- ImageData_Investment_Source::多重性在'ImageData_Investment'关系中的角色'ImageData_Investment_Source'中无效。因为依赖角色属性不是关键属性,所以从属角色的多重性的上界必须是'*'
- ImageData_Investor_Source::多重性在'ImageData_Investor'关系中的角色'ImageData_Investor_Source'中无效。由于依赖性角色属性不是关键属性,所以从属角色的多重性的上限必须为'*'
有人可以请我提出解决此问题的方法和/或实现最佳方法我需要什么,我真的很感激。 由于
尝试改变''INT在FKS为''长(或'long'中的PK到'int') –
后?使得在课堂上的变化(O狂InvestoerId和InvestmentId中的ImageData类是长),我的第二个和第四个错误,走了,现在我只剩下错误#1和3 –
你有相关的这里没有显示流利的配置? –