2012-10-20 66 views
1

我无法理解如何在我正在构建的项目上的类之间创建关系。实体框架5代码第一关系

我有一个类Photo具有与PhotoExif所需一对一的关系,并Photo具有FeaturedPhoto可选的一对一的关系。

,我发现了错误:

Unable to determine composite primary key ordering for type Website.Models.PhotoExif . Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

帮助将非常感激。

Photo.cs

public class Photo 
{ 
    [Key] 
    public int PhotoID { get; set; } 

    public string Title { get; set; } 

    public string Description { get; set; } 
    public Orientation Orientation { get; set; } 
    public int Rating { get; set; } 
    public string URL { get; set; } 
    public string Filename { get; set; } 
    public DateTime DateAdded { get; set; } 
    public bool Hide { get; set; } 
    public string MetaDescription { get; set; } 
    public string MetaKeywords { get; set; } 

    public virtual PhotoExif PhotoExif { get; set; } 
} 

PhotoExif.cs

public class PhotoExif 
{ 
    [Key] 
    public int PhotoExifID { get; set; } 

    public int PhotoID { get; set; } 

    public string ShutterSpeed { get; set; } 
    public string Aperture { get; set; } 
    public string FocalLength { get; set; } 
    public int ISO { get; set; } 
    public string ExposureBias { get; set; } 
    public bool Flash { get; set; } 
    public string WhiteBalance { get; set; } 
    public string Lens { get; set; } 
    public DateTime DateTaken { get; set; } 
    public float Longitude { get; set; } 
    public float Latitude { get; set; } 
    public int Zoom { get; set; } 
    public string Location { get; set; } 

    public virtual Photo Photo { get; set; } 
} 

FeaturedPhoto.cs

public class FeaturedPhoto 
{ 
    [Key] 
    public int FeaturedPhotoID { get; set; } 

    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 
    public string InformationLocation { get; set; } 
    public string ImagePosition { get; set; } 

    public virtual Photo Photo { get; set; } 
} 
+1

您似乎缺少Photo中FeaturedPhoto的导航属性,但除此之外,这里实际上有什么错误?你有错误吗?导航属性不起作用吗? – control

+2

http://stackoverflow.com/a/11914424/114029 –

回答

1

作为每ERR或消息:

Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

需要添加[柱(订单= “#”)]注释到PhotoExif表的PHOTOID和PhotoExifID性质。

+0

为什么选择投票? – mclaassen

+0

我会给你一个upvote导致你的解决方案实际上解决了我的问题。谢谢 – Marco

0

对我来说,看起来你不想在PhotoExif上使用复合主键。我不知道EF为什么试图推断组合键,但原因可能是1)Photo属性按照约定将PhotoID属性设置为外键,2)在一对一关系中,外键必须是与主键相同,3)还有另一个属性PhotoExifID您标记了一个键。因此,EF可能假定这个标记的关键字加上来自一对一关系的传递关键字一起形成一个复合关键字。 (这种行为会很奇怪,但我看不出你的模型和你的注释是否会导致关于复合键排序的异常。)

无论如何,PhotoID属性看起来不正确,因为在一个一对一关系主体和从属关系必须共享相同的主关键字,并且该关联关系的FK同时是PK。我会尝试删除该属性并添加FK属性:

public class PhotoExif 
{ 
    [Key] 
    public int PhotoExifID { get; set; } 

    public string ShutterSpeed { get; set; } 
    //... 

    [ForeignKey("PhotoExifID")] 
    public virtual Photo Photo { get; set; } 
} 

同样必须定义为FK FeaturedPhoto,否则EF不能确定什么是主,什么是从属的关系。取决于关系的细节 - 它们是必需的吗?是必需的吗?必需吗?可选的?可选的?可选的?可选的?哪些实体是主体?哪些是相关的? - 可能需要使用Fluent API定义映射,因为数据注释不支持Fluent API所做的每个映射选项。