具有简单数组的实体框架和模型 [英] Entity Framework and Models with Simple Arrays

查看:66
本文介绍了具有简单数组的实体框架和模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型类。

public class Lead
{
    private readonly ObservableCollection<String> m_tags = new ObservableCollection<string>();

    public int LeadId { get; set; }
    public string Title { get; set; }
    public ObservableCollection<String> Tags { get { return m_tags; } }

}

实体框架是否提供一种使用以下方法表示此方法的方法

Does Entity Framework offer a way to represent this using either Model-First or Code-First?

编辑:我正在寻找一种无需更改模型的公共API的方法。下游开发人员不应该看到标签表的事实。

I'm looking for a way to do this without changing the public API of the model. The fact that there is some sort of Tags table shouldn't be visible to the downstream developer.

推荐答案

因为您的模型必须如果以关系方式表示,则只能使用原始类型(在SQL DB中具有等效形式)或实体定义内的其他实体-这意味着标记由它们自己的实体表示。在您的情况下,使用代码优先方法将是这样的:

Since your model has to be represented in a relational way, you can only use primitive types (that have an equivalent in a SQL DB) or other entities within a entity definition - that means the tags are represented by their own entity. In your case it would be something like this using Code first approach:

public class Lead
{
    public int LeadId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int TagId { get; set; }
    public string Name { get; set; }
}

public class SomeContext : DbContext
{
    public DbSet<Lead> Leads { get; set; }
    public DbSet<Tag> Tags { get; set; }
}

(默认情况下)这将在数据库中表示为表Leads ,一个表标签以及一个仅包含 {LeadId,TagId} 对的关系表LeadTags。

This (by default) will be represented in the database as a table Leads, a table Tags, and a relationship table LeadTags that only contains {LeadId, TagId} pairs.

这篇关于具有简单数组的实体框架和模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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