一对一的关系,实体框架 [英] One-to-one relationship, entity framework

查看:153
本文介绍了一对一的关系,实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在路线和难度级别之间建立一对一的关系(简单,坚硬)



我有这些模型:



I want a one-to-one relationship, between route and difficult level(simple, hard)

I have these models:

public class Route
    {

        [Key]
        public int routeID { get; set; }
        public string Name { get; set; }

        public int? UserProfileID { get; set; }
        public int? CountryID { get; set; }
        //public int? DifficultGradeID { get; set; }



        public virtual UserProfile userProfile { get; set; }
        public virtual Country country { get; set; }
        public virtual Difficult difficult { get; set; }
    }



public class Difficult
    {
        [Key, ForeignKey("Route")]
        public int routeID { get; set; }
        public string DifficultName { get; set; }

        public virtual Route route { get; set; }

    }





但每次我做Update-Databse时,都会收到此错误:





but every time I do Update-Databse, I get this error:

Quote:

类型'ContosoUniversity'属性'routeID'上的ForeignKeyAttribute .Models.Difficult'无效。在依赖类型'ContosoUniversity.Models.Difficult'上找不到导航属性'Route'。 Name值应该是有效的导航属性名称。

The ForeignKeyAttribute on property 'routeID' on type 'ContosoUniversity.Models.Difficult' is not valid. The navigation property 'Route' was not found on the dependent type 'ContosoUniversity.Models.Difficult'. The Name value should be a valid navigation property name.





谢谢



Thank you

推荐答案

您只需要使外键名称与导航属性匹配。

在外键中,您使用了 R oute,导航属性为 r oute

所以改变这个:

You just need to make the foreign key name match the navigation property.
In the foreign key, you have used "Route", and the navigation property is "route"
So change this:
[Key, ForeignKey("Route")]



到此:


to this:

[Key, ForeignKey("route")]



您应该排序。


And you should be sorted.


这篇关于一对一的关系,实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆