[英]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
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过滤行?
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: 优点:
is_deleted = 1
. is_deleted = 1
的实体。 Cons: 缺点:
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. DeleteObject
on the context or a set will do hard delete in the database. DeleteObject
会在数据库中进行硬删除。 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:
此示例显示以下功能:
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; }
}
No, you're going to need the field visible if you want to do something like filter on it, unless you use Stored Procedures. 不,如果您想对其进行过滤等操作,则需要该字段可见,除非您使用存储过程。
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.