代码首先CTP4 - 如何进行一对一映射? [英] Code first CTP4 - How to do one to one mapping?
问题描述
我们有一对一的映射类似于此条目所描述的映射,但是我无法使用代码在CTP4中使用它第一种方法。
We have a one to one mapping similar to that described by this entry, however I have been unable to get this to work in CTP4 with the code first approach.
例如这是我们的情况 - 并且都映射到现有数据库中的各个表,并且员工联系人详细信息是可选的(我已经从类中转移了所有额外的属性),并且数据库在两个表中使用相同的EmployeePK
值。
public class EmployeeContact {
public class EmployeeContact {
[关键] [必填]
public virtual int EmployeeOID {
get 跨度>; set ; }¥b $ b 公共虚拟员工员工 {
get ; set ; }
}
public 班级员工{
[Key] [Required]
public virtual int EmployeeOID {
get; set; }
public virtual Employee Employee {
get; set; }
}
public class Employee {
[Key] [必填] [StoreGenerated(StoreGeneratedPattern.Identity)]
public virtual int EmployeeOID {
get 跨度>; set ; }¥b $ b public virtual EmployeeContact ContactDetails {
get ; set ; } b $ b}
[Key][Required][StoreGenerated(StoreGeneratedPattern.Identity)]
public virtual int EmployeeOID {
get; set; }
public virtual EmployeeContact ContactDetails {
get; set; }
}
我们一直在使用映射类,例如
We have been using mapping classes e.g.
public 类 EmployeeMapping:EntityConfiguration< Employee> {
public class EmployeeMapping: EntityConfiguration<Employee> {
public EmployeeMapping()
public EmployeeMapping()
{
this.HasKey(p => p.EmployeeOID);
this.HasKey( p => p.EmployeeOID);
this.MapSingleType(p => new {
this.MapSingleType( p => new {
EmployeePK = p.EmployeeOID ,
EmployeePK = p.EmployeeOID,
...
})。ToTable(" Emp.Employee");
}).ToTable("Emp.Employee");
&NBSP; }
}
我尝试过使用RelatedTo()数据注释并使用this.HasOptional< EmployeeContact>(p = > p.ContactDetails).WithOptionalDependent(p => p.Employee);在这种情况下,联系人详细信息是可选的。
}
I have tried using the RelatedTo() data annotations and using this.HasOptional<EmployeeContact>( p => p.ContactDetails).WithOptionalDependent( p => p.Employee); The contact details optional in this case.
它们似乎都不起作用。它不会生成正确的SQL,因为它尝试从名为emp.ContactDetails_Employee的表中读取
None of it seems to work. It does not generate the correct SQL, as it attempts to read from a table called emp.ContactDetails_Employee
谢谢
Myles。
推荐答案
Hi Myles
Hi Myles
我一直在为类似的问题挣扎,以下结果证明是解决方案:
I've been striggling with a similar problem for which the following turned out to be a solution:
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.Infrastructure;
namespace ERTest
{
public class Customer
{
public int CustomerID
{
get;
set;
}
public string Name
{
get;
set;
}
public CustomerDetail Details
{
get;
set;
}
}
public class CustomerDetail
{
public int CustomerDetailID
{
get;
set;
}
public int Age
{
get;
set;
}
public Customer Customer
{
get;
set;
}
}
public class CustomerDetailMapping : EntityConfiguration<CustomerDetail>
{
public CustomerDetailMapping()
{
MapSingleType
(
d =>
new
{
d.CustomerDetailID,
d.Age,
d.Customer.CustomerID
}
)
.ToTable( "CustomerDetails" );
}
}
public class CustomerMapping : EntityConfiguration<Customer>
{
public CustomerMapping()
{
HasRequired( c=> c.Details ).WithRequiredDependent( d => d.Customer );
}
}
public class TestContext : DbContext
{
static TestContext()
{
Database.SetInitializer<TestContext>( null );
}
public TestContext( DbModel model )
: base( model )
{
}
public DbSet<Customer> Customers
{
get;
set;
}
public DbSet<CustomerDetail> CustomerDetails
{
get;
set;
}
}
class Program
{
static void Main( string[] args )
{
ModelBuilder builder = new ModelBuilder();
builder.Configurations.Add( new CustomerMapping() );
builder.Configurations.Add( new CustomerDetailMapping() );
TestContext context = new TestContext( builder.CreateModel() );
foreach ( var c in context.Customers.Include( "Details" ) )
{
Console.Out.WriteLine( "{0} {1} {2}", c.CustomerID, c.Name, c.Details.Age );
}
}
}
}
这篇关于代码首先CTP4 - 如何进行一对一映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!