EF中同一个表的外键 - 获取错误

时间:2022-02-10 21:52:24

Basically I got the following table:

基本上我得到了下表:

    [Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    [Required]
    public string UserName { get; set; }
    [Required]
    public string AffiliateId { get; set; }
    [ForeignKey("UserId")]
    public UserProfile Referer { get; set; }
}

Which has a foreign key to itself (Referer). The issue is however the fact that when I try to insert a new row:

哪个有自己的外键(Referer)。但问题是,当我尝试插入新行时:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new { AffiliateId = affiliateId, Referer = referer });

The variable "referer" here is an actual UserProfile. I get the following error: No mapping exists from object type UserProfile to a known managed provider native type.

这里的变量“referer”是一个实际的UserProfile。我收到以下错误:从对象类型UserProfile到已知的托管提供程序本机类型不存在映射。

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


1  

I assume that you are trying to reference a different UserProfile object as the referrer, in which case you don't want to use your primary key (UserId) as the value passed to a ForeignKey attribute. Instead, you should either declare another int property to serve as the FK for the Referrer object, and use ForeignKey to tell EF to use that as the FK for the Referer navigation property, or simply don't use ForeignKey at all and let EF generate the key column for you:

我假设您尝试引用另一个UserProfile对象作为引用者,在这种情况下,您不希望使用主键(UserId)作为传递给ForeignKey属性的值。相反,您应该声明另一个int属性作为Referrer对象的FK,并使用ForeignKey告诉EF将其用作Referer导航属性的FK,或者根本不使用ForeignKey并让EF生成关键栏目:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    // ...
    public UserProfile Referer { get; set; } // EF will generate Referer_UserId
}

(The ForeignKey attribute is used to tell entity framework how to match up a navigation property with the corresponding FK column - it's not used to identify the primary key of the referenced object).

(ForeignKey属性用于告诉实体框架如何将导航属性与相应的FK列匹配 - 它不用于标识引用对象的主键)。

Now, that being said, I'm not sure that's causing your present error:

现在,话虽如此,我不确定是否会导致您目前的错误:

No mapping exists from object type UserProfile to a known managed provider native type.

Check to be sure that there is a DbSet<UserProfile> UserProfiles defined inside the DbContext object in AccountModels.cs. And if your UserProfile object actually refers to any other types that you didn't show in your sample code above, be sure there is a DbSet<> for those as well.

检查以确保在AccountModels.cs中的DbContext对象中定义了DbSet UserProfiles。如果您的UserProfile对象实际上引用了您未在上面的示例代码中显示的任何其他类型,请确保这些类型的DbSet <>也是如此。

#2


0  

Because you are passing the profile, should the syntax not be referer.UserId ?

因为您传递的是配置文件,所以语法不应该是referer.UserId吗?

#1


1  

I assume that you are trying to reference a different UserProfile object as the referrer, in which case you don't want to use your primary key (UserId) as the value passed to a ForeignKey attribute. Instead, you should either declare another int property to serve as the FK for the Referrer object, and use ForeignKey to tell EF to use that as the FK for the Referer navigation property, or simply don't use ForeignKey at all and let EF generate the key column for you:

我假设您尝试引用另一个UserProfile对象作为引用者,在这种情况下,您不希望使用主键(UserId)作为传递给ForeignKey属性的值。相反,您应该声明另一个int属性作为Referrer对象的FK,并使用ForeignKey告诉EF将其用作Referer导航属性的FK,或者根本不使用ForeignKey并让EF生成关键栏目:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    // ...
    public UserProfile Referer { get; set; } // EF will generate Referer_UserId
}

(The ForeignKey attribute is used to tell entity framework how to match up a navigation property with the corresponding FK column - it's not used to identify the primary key of the referenced object).

(ForeignKey属性用于告诉实体框架如何将导航属性与相应的FK列匹配 - 它不用于标识引用对象的主键)。

Now, that being said, I'm not sure that's causing your present error:

现在,话虽如此,我不确定是否会导致您目前的错误:

No mapping exists from object type UserProfile to a known managed provider native type.

Check to be sure that there is a DbSet<UserProfile> UserProfiles defined inside the DbContext object in AccountModels.cs. And if your UserProfile object actually refers to any other types that you didn't show in your sample code above, be sure there is a DbSet<> for those as well.

检查以确保在AccountModels.cs中的DbContext对象中定义了DbSet UserProfiles。如果您的UserProfile对象实际上引用了您未在上面的示例代码中显示的任何其他类型,请确保这些类型的DbSet <>也是如此。

#2


0  

Because you are passing the profile, should the syntax not be referer.UserId ?

因为您传递的是配置文件,所以语法不应该是referer.UserId吗?