分离实体框架poco和objectcontext [英] Separating entity framework poco and objectcontext

查看:104
本文介绍了分离实体框架poco和objectcontext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我正在创建一个classLibrary项目,并从exixting数据库中插入新的Ado.net Entity数据模型和genareting。类和对象类代码正在自动创建。
这对我来说并不重要。



但是我想这样做,将ObjectContext类(例如SomeEntities)和表classess分成两个calss库。



当我更改数据库表属性时,我将更新edmx模型,类将自动更新。



有什么办法吗?
i我没有使用codefirst因为有一个数据库和数据,
i我没有使用modelfirst同样,
i使用databasefirst但不能分开

解决方案

由于您的标题中有poco,我猜你使用的是EF4 POCO生成器T4模板。



那么是的,你可以将POCO类和ObjectContext分成两个不同的类库。 T4模板是为这种情况准备的,因为它由两个不同的文件组成:




  • POCOGenerator.Context.tt - >负责创建派生的ObjectContext

  • POCOGenerator.tt - >负责创建您的POCO实体



如果您在具有EDMX文件的类库中添加了POCO生成器,则默认情况下,两个tt文件都将添加到其中。



但是您可以将第二个文件( POCOGenerator.tt )移动到另一个类库中。 (上下文所在的EDMX项目需要引用此库来识别POCO类)。然后在文本编辑器中打开此文件。这个文件的一些第一行将如下所示:

  ... 
string inputFile = @MyModel。 EDMX;
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
...

您现在需要更改edmx文件的路径(仅在POCOGenerator.tt,保持POCOGenerator.Context.tt不变)。假设您在同一个Visual Studio解决方案中有edmx项目和POCO项目,新的路径可能是:

  ... 
string inputFile = @.. \..\MyEDMXProject\MyModel.edmx;
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
...

现在,您可以从两个不同的项目分别执行这两个文件。一个将创建上下文文件,另一个将创建您的POCO文件。


so far i was creating a classLibrary project and inserting new Ado.net Entity data model and genareting from exixting database. Class and object class codes are creating automatically. this is not important for me.

but i want to do this and separate the ObjectContext class (ex: SomeEntities) and table classess to two calss library.

when i change the database tables property, i will update edmx model and classes will update automatically.

is there any way to do this? i am not using codefirst because have a database and datas in it, i am not using modelfirst likewise, i am using databasefirst but can not separate

解决方案

Since you have "poco" in your title I guess that you are using the EF4 POCO Generator T4 Template.

Then yes, you can separate POCO classes and ObjectContext into two different class libraries. The T4 Template is prepared for that scenario since it consists of two different files:

  • POCOGenerator.Context.tt -> responsible to create your derived ObjectContext
  • POCOGenerator.tt -> responsible to create your POCO entities

If you add the POCO generator in the class library where you have your EDMX file, by default both tt-files will be added there.

But you can move then the second file (POCOGenerator.tt) into another class library. (The EDMX project where the context is located needs to reference this library to recognize the POCO classes.) After that open this file in a text editor. Some of the first lines in this file will look like:

...
string inputFile = @"MyModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
...

You now need to change the path to the edmx file (only in POCOGenerator.tt, leave POCOGenerator.Context.tt unchanged). Assuming you have edmx project and POCO project in the same solution of Visual Studio, the new path might be:

...
string inputFile = @"..\..\MyEDMXProject\MyModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
...

Now you can execute both files separately from two different projects. One will create the context file and the other will create your POCO files.

这篇关于分离实体框架poco和objectcontext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆