简体   繁体   中英

How can I use my own classes as edmx model classes?

I am using 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.

When I try to perform a select I get 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.

What should I do or configurate to link my classes with edmx.

ps: my classes already have their class and properties names equal to edmx ones.

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

    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.

  • Now right click in Edmx > Properties

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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