简体   繁体   English

edmx映射问题

[英]edmx mapping questions

I have a Branch table that contains: 我有一个分支表,其中包含:
company_id, is_deleted, branch_id, branch_name, branch_code company_id,is_deleted,branch_id,branch_name,branch_code

company_id - used in order to determine which company owns the branch. company_id-用于确定哪个公司拥有分支机构。
is_deleted - rows where is_deleted=true are logically deleted and I don't want to return them in my queries. is_deleted-在逻辑上删除is_deleted = true的行,我不想在查询中返回它们。

I have to map thos fields to class Branch. 我必须将这些字段映射到类Branch。 Class Branch have the following members: 类分支具有以下成员:
BranchId, BranchName, BranchCode BranchId,BranchName,BranchCode

  1. Should I add IsDeleted member in order to map the is_deleted field? 是否应该添加IsDeleted成员以映射is_deleted字段? Can I filter rows with is_deleted=true if I will not map this field? 如果不映射此字段,是否可以使用is_deleted = true过滤行?

  2. Should I add CompanyId member in order to map the company_id field? 是否应该添加CompanyId成员才能映射company_id字段? I have many tables with company_id field since it decide whice company own the row. 我有很多带有company_id字段的表,因为它决定了公司拥有的行。 Can I prevent adding CompanyId member when mapping those tables? 映射这些表时,可以阻止添加CompanyId成员吗? When inserting, I need to supply CompanyId - I really prefer to supply it externaly and not from the Branch object. 插入时,我需要提供CompanyId-我真的更喜欢从外部提供它,而不是从Branch对象提供。

So now you have a concrete example so we can continue in discussion from your previous question where I described some basic information about mapping to existing objects. 现在,您有了一个具体的示例,以便我们可以从上一个问题继续进行讨论, 在该问题中 ,我描述了一些有关映射到现有对象的基本信息。

Should I add IsDeleted member in order to map the is_deleted field? 是否应该添加IsDeleted成员以映射is_deleted字段? Can I filter rows with is_deleted=true if I will not map this field? 如果不映射此字段,是否可以使用is_deleted = true过滤行?

It is possible. 有可能的。 It's called conditional mapping where your is_delete column will be used as a filter in the mapping. 这称为条件映射,其中is_delete列将用作映射中的过滤器。 It has pros and cons: 它具有优点和缺点:

Pros: 优点:

  • The filter is applied every time you query the entity set including a lazy loading and an eager loading. 每当查询实体集(包括延迟加载和急切加载)时,都会应用该过滤器。 You will never get an entity with is_deleted = 1 . 您永远不会获得is_deleted = 1的实体。

Cons: 缺点:

  • You can't map is_deleted as a property in the entity. 您不能将is_deleted映射为实体中的属性。 This is one global disadvantage for all columns used to support conditional mapping, table per hierarchy inheritance and independent associations - they can't be exposed as properties. 对于用于支持条件映射,按层次结构继承表和独立关联的所有列而言,这是一个全局缺点。它们不能作为属性公开。 So how would you soft delete your entity if you don't have the column exposed and you can't set it in the application? 那么,如果您没有公开该列并且无法在应用程序中进行设置,那么您将如何软删除您的实体? The only solution for this is stored procedure mapped to delete operation for your entity - btw. 唯一的解决方案是映射到实体删除操作的存储过程-btw。 it is probably the best solution if you want to do soft / logical deletes because otherwise accidental call of DeleteObject on the context or a set will do hard delete in the database. 如果要进行软/逻辑删除,这可能是最好的解决方案,因为否则,在上下文或集合上意外调用DeleteObject会在数据库中进行硬删除。
  • You can't map multiple conditional entities to the same table. 您不能将多个条件实体映射到同一张表。 It means you can't have conditionally mapped both undeleted and deleted entity. 这意味着您不能有条件地映射未删除和删除的实体。 This can be handled by table per hierarchy inheritance. 可以通过表的每个层次结构继承来处理。

Btw. 顺便说一句。 as I know this is not available in DbContext API (EF 4.1). 据我所知,这在DbContext API(EF 4.1)中不可用。

Should I add CompanyId member in order to map the company_id field? 是否应该添加CompanyId成员才能映射company_id字段? I have many tables with company_id field since it decide which company own the row. 我有许多带有company_id字段的表,因为它决定了哪家公司拥有该行。 Can I prevent adding CompanyId member when mapping those tables? 映射这些表时,可以阻止添加CompanyId成员吗? When inserting, I need to supply CompanyId - I really prefer to supply it externaly and not from the Branch object. 插入时,我需要提供CompanyId-我真的更喜欢从外部提供它,而不是从Branch对象提供。

Do you have a relation between the company table and the branch table in your database? 您的数据库中的公司表和分支表之间有关系吗? In such case your Branch entity must use either independent or foreign key association with the Company entity. 在这种情况下,您的Branch实体必须与Company实体使用独立或外键关联。 Association by default creates navigation property on both related entities so your Company entity will have collection of related Branches and your Branch will have a reference to the Company it belongs to. 关联默认情况下会在两个相关实体上创建导航属性,因此您的公司实体将具有相关分支机构的集合,而您的分支机构将具有对其所属公司的引用。 Navigation properties are the main way how to create relations in the object world. 导航属性是在对象世界中创建关系的主要方法。 So if you want the Branch to belong to any Company you will either assign the Company to the property in the Branch or add the Branch to the collection of branches in the Company. 因此,如果您希望分支机构属于任何公司,则可以将公司分配给分支机构中的财产,也可以将分支机构添加到公司中分支机构的集合中。 That is the theory - it is little bit more complex with EF when using detached objects. 这就是理论-使用分离对象时,EF有点复杂。

To avoid some problems EFv4 introduced foreign key association where dependent entity doesn't have only navigation property but also foreign key property (your country_id). 为了避免出现某些问题,EFv4引入了外键关联,其中从属实体不仅具有导航属性,还具有外键属性(您的country_id)。 You can create relation simply by assigning this property with the id of related Country. 您可以简单地通过为该属性分配相关国家(地区)的ID来创建关系。

I have already answered separate question describing differences between Independent and Foreign key associations . 我已经回答了描述独立和外键协会之间差异的单独问题。

Conclusion: You must use either navigation property or foreign key property to create relation between object - both these artifacts are mapped in the entity. 结论:您必须使用导航属性或外键属性来创建对象之间的关系-这两个工件都映射在实体中。


Now example which will also show some details you asked me yesterday. 现在的例子也将显示您昨天问我的一些细节。 This example shows following features: 此示例显示以下功能:

  • Conditional mapping (When is_deleted = 0 in mapping details) 条件映射(在映射详细信息中is_deleted = 0时)
  • Independent association (I have also already described how to change Independent association to Foreign key association ). 独立关联(我已经描述了如何将“独立关联”更改为“外键关联” )。 If you are creating the model from existing database you can check Include foreign key columns in the model in Update wizard and it will use foreign key associations instead of independent associations in the whole model. 如果要从现有数据库创建模型,则可以在“更新”向导中选中“ 在模型包括外键列” ,它将在整个模型中使用外键关联而不是独立关联。
  • Navigation properties on both sides of the relation 关系两侧的导航属性
  • Renaming properties in conceptual model (check mapping details where nice names are mapped to database names) 重命名概念模型中的属性(检查好名字映射到数据库名称的映射详细信息)
  • Changing accessibility of the Id property setter. 更改Id属性设置器的可访问性。 I already answered similar question where this was required with POCO T4 template but same must be done for custom business objects. 我已经回答过类似的问题 ,POCO T4模板需要这样做,但是对于自定义业务对象也必须这样做。
  • Support for lazy loading - check virtual keyword used in business object's code for navigation properties. 支持延迟加载-检查业务对象代码中使用的虚拟关键字是否具有导航属性。
  • Support for tracking proxies - check virtual keyword used in business object's code for scalar properties. 支持跟踪代理-检查业务对象代码中使用的虚拟关键字的标量属性。

在此处输入图片说明

Related mapped business objects will look like: 相关的映射业务对象将如下所示:

public class Branch
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual string Code { get; set; }
    public virtual Company Company { get; set; }
}

public class Company
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Branch> Branches { get; set; }
}

And context using these custom business objects can look like: 使用这些自定义业务对象的上下文如下所示:

public class Context : ObjectContext
{
    public Context()
        :base ("name=ModelContainer")
    {
        Companies = CreateObjectSet<Company>();
        Branches = CreateObjectSet<Branch>();

        ContextOptions.LazyLoadingEnabled = true;
        ContextOptions.ProxyCreationEnabled = true;
    }

    public ObjectSet<Company> Companies { get; private set; }
    public ObjectSet<Branch> Branches { get; private set; }
}
  1. No, you're going to need the field visible if you want to do something like filter on it, unless you use Stored Procedures. 不,如果您想对其进行过滤等操作,则需要该字段可见,除非您使用存储过程。

  2. I don't really understand this one. 我真的不明白这一点。 Why would you NOT want company_id visible if you need to use it when inserting? 如果在插入时需要使用company_id,为什么不希望它不可见? It's not going to hurt anything if it's there. 如果有的话,不会伤到任何东西。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM