EF映射到实体上的错误键 [英] EF is mapping to the wrong keys on entities

查看:111
本文介绍了EF映射到实体上的错误键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行一个linq查询时,它试图将SchoolInfo.SchoolInfoId映射到SchoolId.SchoolId。



如何定义正确的映射,以便映射SchoolInfo.SchoolId to School.SchoolId?



这是Code-First。



SQL表

  table School 

int SchoolId not null PK


表SchoolInfo

int SchoolInfoId not null PK
int SchoolId not null FK

模型

  class School 
{
[Key,DatabaseGenerated(DatabaseGeneratedOption .Identity)]
int schoolId;

virtual SchoolInfo SchoolInfo;
}

class SchoolInfo
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
int schoolInfoId;

int schoolId;

虚拟学校
}

modelBuilder.Entity< School>()。HasOptional(a => a.SchoolInfo).WithRequired(a =>学校);


解决方案

更合适的做法是: / p>

数据库:

  TABLE School(
INT SchoolId NOT NULL PK


TABLE SchoolInfo(
INT SchoolId NOT NULL PK - FK

学校模型:

  public class School 
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId {get;组; }

public virtual SchoolInfo SchoolInfo {get;组; }
}

SchoolInfo模式选项1:

  public class SchoolInfo 
{
[Key,ForeignKey(School)]
public int schoolId {get;组; }

公共虚拟学校{get;组; }
}

SchoolInfo模型选项2:

  public class SchoolInfo 
{
[ForeignKey(School)]
public int SchoolInfoId {get;组; }

公共虚拟学校{get;组; }
}

SchoolInfo模型选项3:

  public class SchoolInfo 
{
[Key]
public int schoolId {get;组; }

公共虚拟学校{get;组;
}

//关系:

modelBuilder.Entity< School>()。HasOptional(a => a.SchoolInfo).WithRequired(a = > a.School);

由于您提到的限制,另一种方法是: / p>

您的实际数据库:

  TABLE School(
INT SchoolId NOT NULL PK


TABLE SchoolInfo(
INT SchoolInfoId NULL PK
INT SchoolId NOT NULL FK - WITH UNIQUE CONSTRAINT TO ENSUERE ONE to ONE

学校型号:

  public class School 
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId {get;组; }

public virtual SchoolInfo SchoolInfo {get;组; }
}

SchoolInfo模式选项1:

  public class SchoolInfo 
{
public int schoolInfoId {get;组;

[Key]
public int schoolId {get;组; }

公共虚拟学校{get;组;
}

//关系:

modelBuilder.Entity< School>()。HasOptional(a => a.SchoolInfo).WithRequired(a = > a.School);

SchoolInfo模型选项2(我没有测试):

  public class SchoolInfo 
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolInfoId {get;组; }

[ForeignKey(School)]
public int schoolId {get;组; }

公共虚拟学校{get;组;
}

//关系:

modelBuilder.Entity< School>()。HasOptional(a => a.SchoolInfo).WithRequired(a = > a.School);

您可以看到:



http://www.entityframeworktutorial.net/entity-relationships.aspx
http://www.entityframeworktutorial.net /code-first/configure-one-to-one-relationship-in-code-first.aspx


When I run a linq query it's trying to map the SchoolInfo.SchoolInfoId to the SchoolId.SchoolId.

How do I define the correct mapping so it knows to map SchoolInfo.SchoolId to School.SchoolId?

This is Code-First.

SQL Tables

table School
(
    int SchoolId not null PK
)

table SchoolInfo
(
    int SchoolInfoId not null PK
    int SchoolId not null FK
)

Models

class School
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    int schoolId;

    virtual SchoolInfo SchoolInfo;
}

class SchoolInfo
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    int schoolInfoId;

    int schoolId;

    virtual School School
}

modelBuilder.Entity<School>().HasOptional(a => a.SchoolInfo).WithRequired(a => a.School);

解决方案

A more appropriate way to do is something like:

Data Base:

TABLE School (
    INT SchoolId NOT NULL PK
)

TABLE SchoolInfo (
    INT SchoolId NOT NULL PK -- FK
)

School Model:

public class School
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int schoolId { get; set; }

    public virtual SchoolInfo SchoolInfo { get; set; }
}

SchoolInfo Model Option 1:

public class SchoolInfo
{
    [Key, ForeignKey("School")]
    public int schoolId { get; set; }

    public virtual School School { get; set; }
}

SchoolInfo Model Option 2:

public class SchoolInfo
{
    [ForeignKey("School")]
    public int SchoolInfoId { get; set; }

    public virtual School School { get; set; }
}

SchoolInfo Model Option 3:

public class SchoolInfo
{
    [Key]
    public int schoolId { get; set; }

    public virtual School School { get; set; }
}

// Relationship:

modelBuilder.Entity<School>().HasOptional(a => a.SchoolInfo).WithRequired(a => a.School);

An alternative way because of the restrictions you mentioned is something like:

Your actual Data Base:

TABLE School (
    INT SchoolId NOT NULL PK
)

TABLE SchoolInfo (
    INT SchoolInfoId NULL PK
    INT SchoolId NOT NULL FK -- WITH UNIQUE CONSTRAINT TO ENSUERE ONE TO ONE
)

School Model:

public class School
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int schoolId { get; set; }

    public virtual SchoolInfo SchoolInfo { get; set; }
}

SchoolInfo Model Option 1:

public class SchoolInfo
{
    public int schoolInfoId { get; set; }

    [Key]
    public int schoolId { get; set; }

    public virtual School School { get; set; }
}

// Relationship:

modelBuilder.Entity<School>().HasOptional(a => a.SchoolInfo).WithRequired(a => a.School);

SchoolInfo Model Option 2 (I did not test it):

public class SchoolInfo
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int schoolInfoId { get; set; }

    [ForeignKey("School")]
    public int schoolId { get; set; }

    public virtual School School { get; set; }
}

// Relationship:

modelBuilder.Entity<School>().HasOptional(a => a.SchoolInfo).WithRequired(a => a.School);

You can see:

http://www.entityframeworktutorial.net/entity-relationships.aspx http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

这篇关于EF映射到实体上的错误键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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