Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mapping 实体框架一对一关系插入时出错_Mapping_Foreign Keys_Entity Framework 5_Sql Server 2008r2 Express - Fatal编程技术网

Mapping 实体框架一对一关系插入时出错

Mapping 实体框架一对一关系插入时出错,mapping,foreign-keys,entity-framework-5,sql-server-2008r2-express,Mapping,Foreign Keys,Entity Framework 5,Sql Server 2008r2 Express,我在设置一组实体以使其正常工作时遇到一些问题。我在VS2012中针对SQL Server 2008 R2 Express使用EF v5 生成的数据库似乎一切正常,但我没有成功地写入某些表 我有一个UserProfile对象,定义如下: [Table("UserProfile")] public class UserProfile { [Key] public long UserId { get; set; } [StringLength(56)] public

我在设置一组实体以使其正常工作时遇到一些问题。我在VS2012中针对SQL Server 2008 R2 Express使用EF v5

生成的数据库似乎一切正常,但我没有成功地写入某些表

我有一个
UserProfile
对象,定义如下:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    public long UserId { get; set; }

    [StringLength(56)]
    public string UserName { get; set; }

    public Country Country { get; set; }
    ...
}
[Table("Country")]
public class Country
{
    [Key]
    public int Id { get; set; }

    [StringLength(2)]
    public string CountryCode {  get; set; }

    [StringLength(100)]
    public string CountryName { get; set; }

    public ICollection<UserProfile> UserProfiles { get; set; }
    ... 
}
我还将
国家
实体定义为:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    public long UserId { get; set; }

    [StringLength(56)]
    public string UserName { get; set; }

    public Country Country { get; set; }
    ...
}
[Table("Country")]
public class Country
{
    [Key]
    public int Id { get; set; }

    [StringLength(2)]
    public string CountryCode {  get; set; }

    [StringLength(100)]
    public string CountryName { get; set; }

    public ICollection<UserProfile> UserProfiles { get; set; }
    ... 
}
为了在控制器操作中进行测试,我有以下内容:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new 
{
    Country = new Country { Id = 223, CountryCode = "UK", CountryName = "UK" },
    ...
});
当执行此
WebSecurity
方法时,我得到以下错误:

不存在从对象类型MyApi.Data.Entities.Country到已知托管提供程序本机类型的映射

我尝试设置一个配置文件来指定代码和数据库中的关系,但它仍然不友好

配置如下所示:

public class CountryEntityConfig : EntityTypeConfiguration<Country>
{
    public CountryEntityConfig()
    {
        this.HasMany(x => x.UserProfiles)
            .WithRequired()
            .HasForeignKey(FKey => FKey.Country);            
    }
}
Country.cs

public class Country
{
    public int CountryId { get; set; }

    [StringLength(2)]
    public string CountryCode {  get; set; }

    [StringLength(100)]
    public string CountryName { get; set; }

    public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
公共类国家
{
public int CountryId{get;set;}
[第(2)款]
公共字符串CountryCode{get;set;}
[长度(100)]
公共字符串CountryName{get;set;}
公共虚拟ICollection用户配置文件{get;set;}
}
数据库现在在UserProfile表中包含了两个与CountryId相关的字段,它们与Country表有关系,我仍然得到了原始的错误消息


TIA,

无映射错误可能是因为您没有将国家实体添加到DbContext中

在AccountModels.cs文件中,您将找到一个从DbContext派生的UsersContext,您必须在其中添加一个
DbSet

然而,这里还有其他问题。例如,您正在使用键创建国家表,默认情况下,EF将为国家表提供一个自动生成的标识列。这意味着您不能为ID插入值,它将自动生成


Country可能是一个查找表,您将使用Country值填充它。因此,您可能只需将CountryId设置为所需国家的值

该国家实体已在DbContext中。我看到的例子总是使用类的实例,而不是Id属性。你的意思是我需要在UserProfile类中包含一个
int CountryId
属性,然后让EF计算出来吗?@Jammer-你告诉EF你想插入一个新的国家,因为你正在创建一个新的国家对象。虽然你可以这样做,但这可能不是你想要的。如果您这样做,您就不能提供ID,因为ID是自动生成的。如果您只想创建一个用户并分配一个id,然后,您应该通过用户的CountryID属性来分配它。@Jammer-从您的描述中可以清楚地看出,您已将Country表添加到DbContext中。在进行更改后,我在UserProfile表中有两个字段,一个名为CountrId,另一个名为Country\u CountryID,这两个字段都带有指向Country表的外键。。。我越来越糊涂了!!我的类的设置与此示例完全相同