[英]Is there an alternative to reference tables when using EF6 and SQL Server 2012?
我有一个包含许多引用表的数据库,例如ContentType,其中表只有两列Id和Name。 这些引用表提供外键的数据,例如在Content表中,它们经常用在Where子句中
我们首先使用代码,但我们自己编写SQL代码而不使用数据库迁移功能。
是否可以将此数据放入可能只有2行或3行的引用表中。 仍然适用于EF6或6.1的东西。 请注意,我仍然希望保持数据分离,而不是混合数据,例如我不想在与ContentType相同的表中混合WorkType的数据。
如果你只是使用这些表作为查找,你可以用Scott Chamberlain建议的Enums替换它们。 你可以使用Entity Framework Code First这样做:
public enum ContentType
{
Text,
Image,
Video,
Audio,
}
public class Content
{
public int Id { get; set; }
public ContentType { get; set; }
}
public class TestContext : DbContext
{
public DbSet<Content> Contents { get; set; }
public static List<Content> GetImages()
{
using (var context = new TestContext()
{
return context.Contents
.Where(c => c.ContentType == ContentType.Image)
.ToList();
}
}
}
当然,使用枚举的缺点是您必须重新编译应用程序以扩展或更改它们,因此数据库查找可以提供更大的灵活性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.