简体   繁体   English

EF6代码优先映射,其中密钥名称在数据库中不同

[英]EF6 Code First Mapping where key name is different in DB

I am trying to do a "code first" EF model based off an existing db (which I recognize immediately is counter to the concept of an EF code-first approach), but I keep running into a problem, and the error message does not make sense to me, so I'm hoping for some insight. 我正在尝试基于现有数据库创建“代码优先” EF模型(我立即意识到这与EF代码优先方法的概念背道而驰),但我一直遇到问题,并且错误消息没有对我来说很有意义,所以我希望能提供一些见识。

My table is defined like so: 我的表是这样定义的:

CREATE TABLE [dbo].[BackupLocales]
(
    [Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY, 
    [LocaleId] INT NOT NULL constraint fkBackupLocales_Locale references dbo.Locales(Id), 
    [BackupLocaleId] INT NOT NULL constraint fxBackupLocales_BackupLocale references dbo.Locales(Id), 
    [Weight] INT NOT NULL DEFAULT 1
)
go
create unique nonclustered index uxBackupLocales_Locale_Weight on dbo.BackupLocales(LocaleId, [Weight]);

In code, I wrote the following entity class: 在代码中,我编写了以下实体类:

public class BackupLocale
{
    [Key]
    [Column("Id")]
    public Guid Id { get; set; }

    public int Weight { get; set; }

    [Column("LocaleId")]
    public int PrimaryId { get; set; }

    [ForeignKey("PrimaryId")]
    public virtual Locale Primary { get; set; }

    [Column("BackupLocaleId")]
    public int BackupId { get; set; }

    [ForeignKey("BackupId")]
    public virtual Locale Backup { get; set; }
}

... which, as far as I can tell (based off of the samples and documentation I've read), look correct. ...据我所知(根据我已阅读的示例和文档),看起来是正确的。 The only "weirdness", as far as I can see, is the fact that the column name for the Primary locale is different in the database; 据我所知,唯一的“怪异”是数据库中Primary语言环境的列名不同; I wanted to make it more specific in code than it is in the table. 我想使其在代码中比在表中更具体。

Naturally, this does not work when trying to load the entity model (or else why would I be asking a question? :) ) 自然,这在尝试加载实体模型时不起作用(否则我为什么要问一个问题?:))

I get a rather long error message, but the heart of it would be this part: 我收到了一条很长的错误消息,但它的核心是这一部分:

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时发生错误。 See the inner exception for details. 有关详细信息,请参见内部异常。 ---> System.Data.SqlClient.SqlException: Invalid column name 'LocaleId'. ---> System.Data.SqlClient.SqlException:无效的列名称“ LocaleId”。

The problem here is, if I were not using EF, a SqlClient.SqlException with the message "Invalid column name 'LocaleId'" would mean that the column literally did not exist in the SQL server... but that is definitely not the case here. 这里的问题是,如果我没有使用EF,则带有消息“ Invalid column name'LocaleId'”的SqlClient.SqlException将意味着该列实际上在SQL Server中不存在...但是绝对不是这种情况这里。 It does exist... so I'm not sure what's going on here. 它确实存在...所以我不确定这里发生了什么。 The only thing I can figure is the fact that I am changing the name of the property is somehow screwing up the mapping, but of course it could be that I'm just doing something dumb. 我唯一能想到的是,我正在更改属性名称的事实以某种方式破坏了映射,但当然可能是我只是在做一些愚蠢的事情。

Have I misunderstood how the attributes are supposed to work? 我是否误解了属性应该如何工作? Have I configured this incorrectly? 我配置不正确吗? Or am I trying to do something that is not supported in EF6? 还是我正在尝试做EF6不支持的事情?

It looks like this was a Visual Studio problem. 看来这是Visual Studio的问题。 I closed the solution, exited VS, restarted VS, and loaded the project, and it started working. 我关闭了解决方案,退出了VS,重新启动了VS,并加载了项目,然后它开始工作。 :( :(

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

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