Entity Framework Database First regeneration make me lose manual changes

后端 未结 2 1541
梦谈多话
梦谈多话 2021-02-11 01:28

I\'m making a website with MVC .NET.

Since I\'m an old school programmer who learn to design database first, I opted for the Database first approach. I\'m also using \"c

相关标签:
2条回答
  • 2021-02-11 02:15

    Don't edit the generated files. Ever. Just. Don't. Do. It.

    Instead, make your edits in partial files in a different directory. To add attributes, declare a Metadata class at the top of your partial class definition.

    [MetadataType(typeof(BlogMetadata))]
    public partial class Blog
    {
        // it's possible to add logic and non-mapped properties here
    }
    

    Now in your Metadata class, you can define attributes or other logic:

    public class BlahMetadata
    {
        [AllowHtml] 
        public string Content{ get; set; } 
    }
    
    0 讨论(0)
  • 2021-02-11 02:22

    No, there is no way to edit the generated code and not have it be replaced when you regenerate. That's why the code has warnings all over that says NOT TO EDIT IT.

    You can, however, use partial classes to add additional functionality. If you don't know what a partial class is, then read about it here:

    http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.80).aspx

    If you want to add attributes, then you have to use a special kind of partial class called a "buddy class".

    http://hartzer.wordpress.com/2010/01/26/mvc-buddy-class/

    0 讨论(0)
提交回复
热议问题