实体模型:如何将实体模型的生成实体类移动到单独的程序集 [英] Entity Model: How to move Entity Model's generated entity classes to a separate assembly

查看:189
本文介绍了实体模型:如何将实体模型的生成实体类移动到单独的程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从我的数据库生成了实体模型,它创建了实体类。



1)有没有办法为每个类创建单独的文件?
2)我们可以将这些类移动到不同的程序集吗?例如,如何在我的解决方案中将这些类移动到另一个项目?



我想将实体模型与类分离,并使用表示层中的类。 p>

解决方案

您可以在单独的项目中创建 edmx 不要认为您可以将类分成多个生成的默认文件。请注意,您不应该尝试编辑生成的类,因为这些类将在更新时被覆盖,并且您可以轻松地中断此类。



然而,您可以创建一个 public partial class 与每个Entity对象具有相同的命名空间和类声明,以允许您扩展类并添加初始化/验证功能等,以便将每个实体放在自己的类文件扩展。



更新:



所有相关的部分类必须在同一个程序集中,需要在< EntityModel> .Designer.cs partial class 相同的命名空间和类声明>。见下面的代码示例。

 命名空间YourEntityNamespace 
{
public partial class YourEntity:EntityObject
{
//添加扩展实体类的方法和属性
}
}


I have generated entity model from my database which created entity classes.

1) Is there a way that it creates separate file for each class? 2) Can we move these classes to a different assembly? For example how can I move these classes to a different project in my solution?

I want to separate the entity model from classes and use the classes in Presentation Layer.

解决方案

You can of course create the edmx in a separate project but I don't think you can separate the classes into more than the generated default files. Note that you shouldn't attempt to edit the generated classes as these will be overwritten when updated and you can easily break stuff too.

You can, however, create a public partial class with the same namespace and class declaration as each of the Entity objects to allow you to extend the classes and add initialization / validation functions etc. allowing you to put each entity in its own class file for extending.

Updated:

All related partial classes must be in the same assembly and need to use the same namespace and class declaration as the original entity partial class in the <EntityModel>.Designer.cs. See below for example code.

namespace YourEntityNamespace
{
    public partial class YourEntity : EntityObject
    {
        // Add methods and properties to extend the entity class
    }
}

这篇关于实体模型:如何将实体模型的生成实体类移动到单独的程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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