简体   繁体   English

实体框架-首先在模型中,如何使我的EF类基于我已有的现有域类?

[英]Entity Framework - in model first, how do I get my EF classes to be based on the existing domain classes I have?

If I already have domain classes, and I want to be able persist them using EF via a model first approach how do I do this? 如果我已经拥有域类,并且希望能够通过模型优先方法使用EF来持久化它们,该如何做?

For example do I go to the EF designer (in VS2010) and create the model and generate the classes, then go to these EF classes and somehow manually modify them? 例如,是否要去EF设计器(在VS2010中)并创建模型并生成类,然后去这些EF类并以某种方式手动修改它们? But then would there be an issue if I needed to change the model and re-create database TSQL from the model for updates? 但是如果我需要更改模型并从模型中重新创建数据库TSQL进行更新,那会不会有问题?

What's the easiest approach? 最简单的方法是什么?

EDIT - In my case I've got some base classes that do graph navigation - I was hoping I could leave (a) my graph traversal routines in the business logic assembly, eg NodeBase, RelationshipBase etc, and then (b) have my EF persistance in a separate EF assembly. 编辑-就我而言,我有一些进行图导航​​的基类-我希望我可以(a)将图遍历例程留在业务逻辑程序集中,例如NodeBase,RelationshipBase等,然后(b)使用我的EF坚持在一个单独的EF程序集中。 Any advice on this specific scenario I have? 我对此有什么建议?

EDIT 2 - Actually I note I note the following template options from "Add Code Generation Item.." 编辑2-实际上,我注意到我注意到“添加代码生成项..”中的以下模板选项。

  1. ADO.NET C# POCO Entity Generator Template - Online ADO.NET C#POCO实体生成器模板-在线
  2. ADO.NET Self-Tracking Entity Generator - Local ADO.NET自跟踪实体生成器-本地
  3. ADO.NET Entity Object Generator ADO.NET实体对象生成器

I'm guessing the #3 one is the default that comes with VS2010. 我猜想#3是VS2010随附的默认值。 Anyone across #1 and #2 and which might be best to assist in my goal? #1和#2中的任何人,哪个可能最有助于实现我的目标? ie Having an existing set of domain classes with methods that already inherit from other classes (ie noting with the current EF generated entity classes they seem to already have to inherit from EntityObject) 即拥有一组现有的域类,这些域类具有已经从其他类继承的方法(即注意当前EF生成的实体类,它们似乎已经必须从EntityObject继承)

The generated classes are all partial. 生成的类都是局部的。 So if you have the same namespace and same Entity/Class names it's fairly easy to merge an existing set of classes with a model-first set of entities. 因此,如果您具有相同的名称空间和相同的实体/类名称,则将现有的类集与模型优先的实体集合并是相当容易的。 Simply put partial on your class and remove all the properties that are implemented by the Entity framework generated code. 只需将partial放在您的类上,并删除由Entity Framework生成的代码实现的所有属性。

暂无
暂无

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

相关问题 如何在MVC中将我自己的Model类与EF实体类结合使用? - How do I use my own Model classes in conjunction with EF entity classes in MVC? 如何使用Entity Framework将POCO模型类映射到现有数据库表 - How do I map POCO model classes to existing DB tables using Entity Framework 如何将我的模型类与MVC 3中的现有数据库绑定? - How do I tie my Model classes with an existing database in MVC 3? 如何获得实体框架以在生成的类上使用接口? - How do I get Entity Framework to use interfaces on the generated classes? 在使用Model-First方法时,如何将XML注释添加到Entity Framework生成的自动生成的类中? - How do I add XML comments to the auto-generated classes produced by Entity Framework when using Model-First approach? 使用实体框架代码优先我是否需要让我的域模型贫血? - Using Entity Framework Code-first am I required to have the model of my domain anemic? 我应该在控制器中使用实体框架类还是模型类? - Should I use entity-framework classes or model classes in the Controllers? 如何在添加的部分类中的实体模型的生成代码中实现OnPropertyChanged事件 - How do I implement the OnPropertyChanged event in the generated code of my entity Model in partial classes that I add 如何将业务逻辑放在我的ADO.NET Entity Framework类中? - How do I put business logic in my ADO.NET Entity Framework classes? 如何将我的存储库从EF代码优先转换为使用EDMX实体模型? - How do I switch my repositories from EF code-first to using a EDMX entity model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM