简体   繁体   English

如何将自己的类用作edmx模型类?

[英]How can I use my own classes as edmx model classes?

I am using EF 6. 我正在使用EF 6。

I'd like to use my own domain classes as edmx model classes, so I erased the tt generated model classes and changed edmx build Action to None. 我想使用自己的域类作为edmx模型类,因此我删除了tt生成的模型类,并将edmx build Action更改为None。

When I try to perform a select I get MetadataException 当我尝试执行select时,我得到MetadataException

DbContext context = new DbContext("name=BaseObjectsEntities");
var u = context.Set<User>().Where(x => x.Id == 1).FirstOrDefault();

Works perfectly with generated tt model classes, but not with my own classes. 与生成的tt模型类完美配合,但不适用于我自己的类。

What should I do or configurate to link my classes with edmx. 我应该做什么或配置我的类与edmx链接。

ps: my classes already have their class and properties names equal to edmx ones. ps:我的类的类和属性名称已经等于edmx了。

I solved my problem, 我解决了我的问题,

These are the steps I did: 这些是我做的步骤:

  • If you still haven't 如果你还没有

    Create your own domain classes; 创建自己的域类;

  • Create Database 创建数据库

    create tables 创建表格

  • Create Edmx 创建Edmx

    Now listen carefully, when you finalize the creating of edmx, visual studio will open edmx in design mode, don't save, don't close, don't do anything but change the name of entity models and their properties (in diagram block that represents the model), make sure the name of entity and It's properties are as specified in domain, I will tell later why not save or close. 现在仔细聆听,当你最终确定edmx的创建时,visual studio将在设计模式下打开edmx,不保存,不关闭,不做任何事情,只更改实体模型的名称及其属性(在图块中)代表模型),确保实体的名称和它的属性是在域中指定的,稍后我将告诉为什么不保存或关闭。

Don't save or close the design window yet, if you do this and your domain classes are in the same project they will be overwritted by the .tt classes that are generated when emdx is created. 不要保存或关闭设计窗口,如果这样做并且您的域类在同一个项目中,它们将被创建emdx时生成的.tt类覆盖。

Delete All .tt file that is agregated to emdx and don't save. 删除聚合到emdx并且不保存的所有.tt文件。

  • Now right click in Edmx > Properties 现在右键单击Edmx> Properties

Erase whatever is writted in "Custom Tool" and make sure the build Action is EntityDeploy 擦除“自定义工具”中写入的内容,并确保构建操作是EntityDeploy

When you remove the text writted in Custom Tool, click enter, then build solution 删除自定义工具中写入的文本时,单击“输入”,然后构建解决方案

Now it will work. 现在它会起作用。

  • I forgot to mention that if these steps does not work, there is a secret step: 我忘了提到如果这些步骤不起作用,则有一个秘密步骤:

delete the project from visual studio and from project folder, recreate it, and restart the initial steps. 从visual studio和项目文件夹中删除项目,重新创建它,然后重新启动初始步骤。

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

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