简体   繁体   English

使用实体框架生成错误的查询

[英]Wrong query generated with entity framework

I need to know what am I doing wrong, because the generated query doesn't match with the attributes of the data base table and I think that my class was well type and also the mappings. 我需要知道我做错了什么,因为生成的查询与数据库表的属性不匹配,我认为我的类是良好的类型,也是映射。 Here's my code 这是我的代码

public class Usuario
{
    #region Atributos
    private int _intID = 0;
    private Perfil _Perfil_FK = null;
    private String _strNombre = "";
    private String _strPassword = "";
    #endregion

    #region Propiedades

    public int ID
    {
        get { return _intID; }
        set { _intID = value; }
    }

    public Nullable<int> IDPerfil_FK { get; set; }

    public virtual Perfil Perfil_FK
    {
        get { return _Perfil_FK; }
        set { _Perfil_FK = value; }
    }
    public String Nombre
    {
        get { return _strNombre; }
        set { _strNombre = value; }
    }
    public String Password
    {
        get { return _strPassword; }
        set { _strPassword = value; }
    }
    #endregion
}

My test was only this _db.Usuario() 我的测试只是这个_db.Usuario()

The Generated Sql query 生成的SQL查询

SELECT 
[Extent1].[IDUsuario] AS [IDUsuario], 
[Extent1].[IDPerfil_FK] AS [IDPerfil_FK], 
[Extent1].[Nombre] AS [Nombre], 
[Extent1].[Password] AS [Password], 
[Extent1].[PerfilID] AS [PerfilID] <-- this attribute doesn't exit's
FROM [dbo].[Usuario] AS [Extent1];

Here's my db context class 这是我的db上下文类

public class MasterPageAtentoDB : DbContext
{
    public DbSet<Pagina> Pagina { get; set; }
    public DbSet<Perfil> Perfil { get; set; }
    public DbSet<Permiso> Permiso { get; set; }
    public DbSet<Usuario> Usuario { get; set; }

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Usuario>().Property(r => r.ID).HasColumnName("IDUsuario");
        modelBuilder.Entity<Pagina>().Property(r => r.ID).HasColumnName("IDPagina");
        modelBuilder.Entity<Permiso>().Property(r => r.ID).HasColumnName("IDPermiso");
        modelBuilder.Entity<Perfil>().Property(r => r.ID).HasColumnName("IDPerfil");
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }
}

My database Table 我的数据库表

数据库表

Entity Framework doesn' recognize your property IDPerfil_FK as the foreign key property for the Perfil_FK navigation property because you are not following the naming conventions required for automatic FK property detection. 实体框架不会将您的属性IDPerfil_FK识别为Perfil_FK导航属性的外键属性,因为您没有遵循自动FK属性检测所需的命名约定。 As a result EF assumes that IDPerfil_FK is an ordinary scalar property and that Perfil_FK has no exposed FK property in your model and the column in the database has the standard name Perfil_ID (navigation property name + "_" + primary key property name of target entity class). 因此,EF假定IDPerfil_FK是普通的标量属性,并且Perfil_FK在模型中没有公开的FK属性,并且数据库中的列具有标准名称Perfil_ID (导航属性名称+“_”+目标实体的主键属性名称类)。

You have three options to fix this: 您有三种方法可以解决此问题:

  • Name the FK property appropriately (navigation property name + primary key property name of target entity class): 适当地命名FK属性(导航属性名称+目标实体类的主键属性名称):

     public Nullable<int> Perfil_FKID { get; set; } 
  • Put a data annotation attribute on the property to indicate that it is a FK property: 在属性上放置数据注释属性以指示它是FK属性:

     [ForeignKey("Perfil_FK")] public Nullable<int> IDPerfil_FK { get; set; } 
  • Define the FK property in Fluent API: 在Fluent API中定义FK属性:

     modelBuilder.Entity<Usuario>() .HasOptional(u => u.Perfil_FK) .WithMany() // or with parameter if Perfil class refers back to Usuario .HasForeignKey(u => u.IDPerfil_FK); 

I would prefer the first option because your mapping of the primary key properties relies on conventions anyway, so it would be consequent to follow the conventions for the foreign key properties too. 我更喜欢第一个选项,因为主键属性的映射无论如何都依赖于约定,因此它也会遵循外键属性的约定。

I believe you can fix this as easy as adding the data annotation alerting it to your primary key. 我相信你可以解决这个问题,就像添加数据注释一样简单,提醒你注意主键。

 [Key]
 public int ID

By default EF will try to match ClassnameId as the key, if that isn't found it will try and match Id as key, otherwise it will throw an error. 默认情况下,EF将尝试匹配ClassnameId作为键,如果没有找到它将尝试匹配Id作为键,否则将引发错误。 It is case sensitive. 它区分大小写。 So if you want to use Uppercase ID you need to explicitly tag it with the [Key] annotation so it knows how to map it. 因此,如果您想使用大写ID,您需要使用[Key]注释显式标记它,以便它知道如何映射它。

http://msdn.microsoft.com/en-us/data/gg193958 for some common annotations. http://msdn.microsoft.com/en-us/data/gg193958用于一些常见注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM