Entity Framework Migrations don't include DefaultValue data annotation (EF5RC)

后端 未结 2 1635
陌清茗
陌清茗 2021-01-04 05:44

I have a class that looks like this:

[Table(\"Subscribers\", Schema = \"gligoran\")]
public class Subscriber
{
    [Key]
    public string Email { get; set;          


        
相关标签:
2条回答
  • 2021-01-04 06:06

    As an Extra to Ladislav's Comment. Which is correct. you cant do it in the model. IF you wanted to use code based migrations. Ie using PM commandlet Add-Migration / Update Database, then this approach introduces a generated class into the process. Then you can have defaults . See the classes that Derive from DBMigrations. see http://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigration.addcolumn%28v=vs.103%29.aspx You can specific Column builder Lamda expressions. This allows defaults.

    namespace MigrationsDemo.Migrations
    {
      using System;
      using System.Data.Entity.Migrations;
    
    public partial class SomeClassThatisATable : DbMigration
    {
        public override void Up()
        {
            AddColumn("MyTable", "MyColumn", c => c.String( defaultvalue:"Mydefault"  ));
        }
    
    0 讨论(0)
  • 2021-01-04 06:17

    EF doesn't use DefaultValue attribute at all = it is not part of the model so migrations don't see it. You can propose support of this annotation on Data UserVoice.

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