我正在使用 EF 6。
我想使用我自己的域类作为 edmx 模型类,所以我删除了 tt 生成的模型类并将 edmx build Action 更改为 None。
当我尝试执行选择时出现 MetadataException
DbContext context = new DbContext("name=BaseObjectsEntities");
var u = context.Set<User>().Where(x => x.Id == 1).FirstOrDefault();
与生成的 tt 模型类完美配合,但不适用于我自己的类。
我应该做什么或配置什么才能将我的类(class)与 edmx 链接起来。
ps:我的类已经有它们的类和属性名称等于 edmx 的。
最佳答案
我解决了我的问题,
这些是我做的步骤:
如果你还没有
Create your own domain classes;
创建数据库
create tables
创建 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.
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.
Delete All .tt file that is agregated to emdx and don't save.
- 现在右键单击 Edmx > 属性
Erase whatever is writted in "Custom Tool" and make sure the build Action is EntityDeploy
When you remove the text writted in Custom Tool, click enter, then build solution
现在它可以工作了。
- 忘了说,如果这些步骤不行,还有一个 secret 步骤:
delete the project from visual studio and from project folder, recreate it, and restart the initial steps.
关于c# - 如何使用我自己的类作为 edmx 模型类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493820/