如何在实体模型类中保留自定义代码更改 [英] How Do I Preserve Custom Code Changes In Entity Model Classes

查看:50
本文介绍了如何在实体模型类中保留自定义代码更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





1)我在我的项目中使用数据库第一种方法,下面是一个小样本来描述我想在实际项目中实现的目标。



2)我从我的数据库中生成了一个实体模型,它生成了以下代码:



< pre lang =cs> namespace 测试
{
使用系统;
使用 System.Collections.Generic;

public partial class 学生
{
public 学生()
{
.Enrollments = new HashSet& amp; lt; Enrollment& amp; gt;();
}

public int StudentID {获得; set ; }
public string LastName { get ; set ; }
public string FirstName { get ; set ; }
public System.DateTime EnrollmentDate { get ; set ; }

public virtual ICollection& amp; lt; Enrollment& amp; gt ;注册{获取; set ; }
}
}





3)我在上面给出的学生班上做了一些修改,我添加了一个属性和属性:



 使用 System.ComponentModel.DataAnnotations; 

[必需()]
public int StudentID {< span class =code-keyword> get ; set ; }

public string FullName {获得; set ; }





4)然后我从数据库从数据库更新模型更新了模型,并丢失了我在数据库中所做的所有更改学生班。



我想知道在从数据库更新模型后是否有任何方法可以保留这些变化?



任何和所有帮助将受到高度赞赏。



谢谢

解决方案

嘿Phil ,



我在线阅读文章,发现了一个名为好友的概念。这是我刚刚编写的代码但尚未测试过:



新类:



 命名空间测试
{
public class StudentMetaData
{

[Required()]
public < span class =code-keyword> int StudentID { get ; set ; }

public string FullName {获得; set ; }

}
}







修改生成的类:



 命名空间测试
{
< span class =code-keyword>使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel.DataAnnotations;

[元数据类型( typeof (StudentMetaData))]
public partial class 学生
{
公开学生()
{
.Enrollments = new 的HashSet<注册>();
}

public int StudentID {获得; set ; }
public string LastName { get ; set ; }
public string FirstName { get ; set ; }
public System.DateTime EnrollmentDate { get ; set ; }

public virtual ICollection< Enrollment>注册{获取; set ; }
}
}





谢谢


这个班级是声明为partial,您可以创建自己的文件,在其中可以使用自己的代码声明相同的部分类。

代码生成器将始终替换原始文件中的代码,没有办法跳过它;相反,它永远不会触及你自定义文件中的任何内容。

你只需要匹配命名空间和类名。



希望这有帮助。


Hi,

1) I am using the Database first approach in my project, below is a small sample to describe what i want to achieve in my actual project.

2) I have generated an entity model from my database which generates the following code:

namespace Test
{
    using System;
    using System.Collections.Generic;

    public partial class Student
    {
        public Student()
        {
            this.Enrollments = new HashSet&amp;lt;Enrollment&amp;gt;();
        }

        public int StudentID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public System.DateTime EnrollmentDate { get; set; }

        public virtual ICollection&amp;lt;Enrollment&amp;gt; Enrollments { get; set; }
    }
}



3) I made some changes in the above given Student class, i added an attribute and a property:

using System.ComponentModel.DataAnnotations;

[Required()]
public int StudentID { get; set; }

public string FullName { get; set; }



4) Then i updated the model from database "Update Model from Database", and lost all the changes which i made in the Student class.

I want to know if there is any way to preserve these changes after updating the model from database?

Any and all help will be highly appreciated.

Thanks

解决方案

Hey Phil,

I was going through articles online and found a concept called "Buddy" classes. Here is the code I just wrote but not tested till yet:

New Class:

namespace Test
{
    public class StudentMetaData
    {

        [Required()]
        public int StudentID { get; set; }

        public string FullName { get; set; }

    }
}




Modified generated class:

namespace Test
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    [MetadataType(typeof(StudentMetaData))]
    public partial class Student
    {
        public Student()
        {
            this.Enrollments = new HashSet<Enrollment>();
        }

        public int StudentID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public System.DateTime EnrollmentDate { get; set; }

        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }
}



thanks


As this class is declared as partial, you have the ability to create your own file, in which you can declare the same partial class, with your own code.
The code generator will always replace the code in the original file, there's no way to skip it; on the contrary, it will never touch anything in your custom own file.
You just have to match namespace and class name.

Hope this helps.


这篇关于如何在实体模型类中保留自定义代码更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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