【问题标题】:How to use Data Annotations inheritance class如何使用 Data Annotations 继承类
【发布时间】:2015-08-12 12:31:46
【问题描述】:

我首先使用mysql和实体框架代码。我有一堂课BaseEntity

public abstract class BaseEntity
{
    [Key, Column(Order = 1)]
    public int id { get; set; }


    [Column(Order = 2)]
    public int siteId { get; set; }


    [Column(Order = 5)]
    public bool online { get; set; }


    [Column(Order = 6)]
    public bool deleted { get; set; }


    [Column(Order = 7)]
    public DateTime addDate { get; set; }


    [Column(Order = 8)]
    public DateTime updateDate { get; set; }


    [Column(Order = 9)]
    public DateTime deletedDate { get; set; }
}

我有Brand,它继承自BaseEntity

 [AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.All, AllowMultiple = false,Inherited = true)]
public class Brand:BaseEntity
{
    [Column(Order = 3)]
    public string brandName { get; set; }
    [Column(Order = 4)]
    public string brandLogo { get; set; }
}

当我要构建项目时出现错误:

Error   1   Attribute 'AttributeUsageAttribute' is only valid on classes derived from System.Attribute

我想使用数据注释对列进行排序。我想在数据库中查看这些列:

 1. id
 2. siteid
 3. brandName
 4. brandLogo
 5. online
 6. deleted
 7. addDate
 8. updateDate
 9. deletedDate

我该如何解决这个问题?

【问题讨论】:

  • 删除[AttributeUsageAttribute ....] 你的类不是属性。
  • 当我删除它时,项目正在构建,但我写了更新数据库,它没有排序。我该怎么做

标签: mysql asp.net-mvc entity-framework ef-code-first data-annotations


【解决方案1】:

我正在使用这个模型/上下文进行测试

public class TestContext : DbContext
{
    public TestContext(DbConnection connection) : base(connection, true) { }

    public DbSet<Brand> Brands { get; set; }

}

public abstract class BaseEntity
{
    [Key, Column(Order = 1)]
    public int id { get; set; }

    [Column(Order = 2)]
    public int siteId { get; set; }

    [Column(Order = 5)]
    public bool online { get; set; }

    [Column(Order = 6)]
    public bool deleted { get; set; }

    [Column(Order = 7)]
    public DateTime addDate { get; set; }

    [Column(Order = 8)]
    public DateTime updateDate { get; set; }

    [Column(Order = 9)]
    public DateTime deletedDate { get; set; }
}

public class Brand : BaseEntity
{
    [Column(Order = 3)]
    public string brandName { get; set; }

    [Column(Order = 4)]
    public string brandLogo { get; set; }
}

static class Test
{
    public static void Run(DbConnection connection)
    {
        using (TestContext db = new TestContext(connection))
        {
            db.Brands.Add(new Brand());
            db.SaveChanges(); // Just to run DB and Table creation statement
        }
    }
}

当然,您必须删除 AttributeUsageAttribute。
将您的类与 EF 6.1.3 和 SQL Server 完全结合使用,它可以按预期工作(表是使用指定顺序的列创建的)。
我使用 Microsoft Access Entity Framework 提供程序进行了尝试(我可以调试它并更好地理解行为,因为我编写了它)并且它有效。
所以这似乎是一个 MySql 实体框架提供程序错误

但是
EF 提供者不应决定列的顺序。他们应该只做一个 foreach (我正在阅读 Microsoft Access 提供程序)并生成一个创建字符串。 所以 EF 提供者这部分的 bug 很奇怪。

您使用的是哪个版本的 EF?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多