将类(从外部源)映射到int表列。复杂类型? [英] Mapping class (from external source) to int table column. Complex Type ?

查看:91
本文介绍了将类(从外部源)映射到int表列。复杂类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有一个名为User的课程,看起来像是这样的b
$
public class User  {

   public int ID {get; set;}

  公共字符串名称{get; set;}

   public  bool IsSystemUser {get; set;}

.....

}



我的实体像这样使用这些

公共类DataEntity {

   public int ID {get; set;}

   public int TransactionType {get; set; }¥b $ b   public User ChangedBy {get; set; }
}



问题是这个用户没有从同一个数据库加载,用户被加载并从外部缓存服务。



之前我们一直在使用NHibernate和IUserType实现。



是否可以除了在每个实体上都有额外的属性之外,以任何其他方式在EF4 CTP5中映射它?

我可以使用Complext类型吗?

Hi

I have a class called User which looks something like

public class User {
   public int ID {get;set;}
   public string Name {get;set;}
   public bool IsSystemUser {get;set;}
.....
}

My entities use these like this
public class DataEntity {
   public int ID {get;set;}
   public int TransactionType{ get;set; }
   public User ChangedBy { get;set; }
}

The problem is that this User is not loaded from the same Database, the Users are loaded and cached from an external service.

Earlier we have been using NHibernate, and a IUserType implementation.

Is it possible to map this in EF4 CTP5 in any way other than having a extra Property on every single Entity ?
Can i use a Complext Type ?

推荐答案

您好,

我认为您希望EF只使用ID属性集创建User实例,并将其分配给DataEntity.User属性, 然后从其他来源加载其他属性? (如果更改了用户属性,则新ID将写入数据库
。)

I think you want EF to create an instance of User with just the ID property set and assign that to the DataEntity.User property, then the other properties are loaded from another source? (and if the user property is changed then the new ID will be written to the database).

您可以使用复杂类型实现此目的,您的配置看起来像这样:

You can achieve this using complex types, your configuration would look something like this:

public class MyContext : DbContext
{
  public DbSet<DataEntity> Entities { get; set; }

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    modelBuilder.ComplexType<User>()
      .Ignore(u => u.IsSystemUser);
    modelBuilder.ComplexType<User>()
      .Ignore(u => u.Name);

    modelBuilder.Entity<DataEntity>()
      .Property(e => e.ChangedBy.ID)
      .HasColumnName("ChangedByUserId");
  }
}

 

~Rowan


这篇关于将类(从外部源)映射到int表列。复杂类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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