简体   繁体   English

实体框架无法从数据库正确创建模型

[英]Entity Framework can't properly create model from database

I have some database that goes quite against most standards (eg some tables have no PKs, some FKs point nowhere...). 我有一些数据库符合大多数标准(例如,某些表没有PK,某些FK却没有指向...)。 One entity, let's call it entity1 has several fields, field1, Field2 and field_3. 一个实体,我们称它为entity1有几个字段,即field1,Field2和field_3。

For some reason, when I try to map it, field1 comes OK, Field2 gives me an error and field_3 is okay as well. 出于某种原因,当我尝试对其进行映射时,field1正常,Field2给我一个错误,field_3也可以。

Field2 has been mapped as follows: Field2的映射如下:

public virtual DbSet<field2> field2 { get; set; }

Manually editing it like this works and fixes the problem altogether: 像这样手动编辑它可以解决所有问题:

public virtual DbSet<Field2> field2 { get; set; }

However I don't know why EF fails to build it correctly, since it's technically reading from the database and there's no other issue related to that. 但是我不知道为什么EF无法正确构建它,因为从技术上讲它是从数据库中读取的,并且没有与此相关的其他问题。 This also belongs to the auto-generated file that contains entities to work with, so I'm a bit afraid my changes might break something, on top of the file being automatically regenerated every time someone updates the model from the database. 这也属于自动生成的文件,该文件包含要使用的实体,因此,每当有人从数据库更新模型时,文件都会自动重新生成,因此我担心我的更改可能会破坏某些内容。

Thanks in advance. 提前致谢。

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MyProject.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Field2
    {
        public int IdFoo { get; set; }
        public int IdBar { get; set; }
        public string Name { get; set; }
        public Nullable<int> IdSomething { get; set; }
        public Nullable<int> Idwhatever { get; set; }
        public string Blah { get; set; }
        public Nullable<int> idBleh { get; set; }
    }
}

I found the answer. 我找到了答案。 Turns out EF6 is plagued with errors including this one. 事实证明EF6受到包括这一错误的困扰。

For this reason, they stopped supporting Database-first models and incentivated Code-first approaches instead of auto-generated classes. 因此,他们不再支持数据库优先模型和激励性代码优先方法,而不是自动生成的类。

In short, move on to EF Core if you can and use Code-first as your go-to procedure. 简而言之,如果可以的话,请继续使用EF Core,并使用“代码优先”作为转到步骤。

public virtual DbSet<field2> field2 { get; set; }

In your above code the first field2 referenced there is a Type while the second is a property / variable (see this link for the difference between variable and Type ). 在上面的代码中,第一个引用的field2有一个Type而第二个是property / variable (请参阅此链接以获取variableType之间的区别)。

The reason that: 原因如下:

public virtual DbSet<field2> field2 { get; set; }

doesn't compile is that you don't have a class called field2 in your codebase. 无法编译是因为您的代码库中没有名为field2的类。

The reason that: 原因如下:

public virtual DbSet<Field2> field2 { get; set; }

does compile is that you do have a class called Field2 in your codebase. 编译的原因是您的代码库中确实有一个名为Field2的类。 The typename is case sensitive . 类型名区分大小写

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

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