现有表和新表之间的流畅映射

发布于 2024-09-19 19:28:58 字数 1009 浏览 2 评论 0原文

问题很简单我有两个使用 Fluent nhibernate 映射的类:

public class A: EntityBase {}
public class B: EntityBase
{
   public virtual A A_Something {get;set;}
}

EntityBase 类仅提供 Key 属性。现在我想映射它们并配置我的数据库。所以这里是映射

public class AMap : DomainEntityBase<A>
{
  public AMap(){}
}

public class BMap : DomainEntityBase<B>
{
  public BMap()
  {
    References(p=>p.A_Something).Column("A_ID");
  }
}

和数据库配置

Fluently.Configure().Database(...).Mappings(m =>
  m.FluentMappings.Add<BMap>()).
   ExposeConfiguration(BuildSchema).BuildSessionFactory();

配置仅使用 BMap 完成,不幸的是表 A 已经存在于数据库中,并且它的数据无法更改(这是因为该应用程序正在使用更大数据库的部分,并且这些部分不可修改)。因此,此模式生成标准异常

An association from the table [B] refers to an unmapped class: A

是否有可能以某种方式在我的配置中指定 A 已存在于数据库中?如果不是,您有什么建议以温和的方式解决这个问题?当然,我可以准备 *.sql 文件来手动创建表 B,但这不是解决方案(只是将问题推开,因为在进一步的开发过程中它肯定会回来)。创建单独的数据库并从第一个数据库导入数据也可以工作,但不幸的是需要将数据存储在一个(且只有一个)数据库中。

Problem is simple I have two classes mapped with fluent nhibernate :

public class A: EntityBase {}
public class B: EntityBase
{
   public virtual A A_Something {get;set;}
}

with EntityBase class providing only Key property. Now i want to map them and configure my db. So here are mappings

public class AMap : DomainEntityBase<A>
{
  public AMap(){}
}

public class BMap : DomainEntityBase<B>
{
  public BMap()
  {
    References(p=>p.A_Something).Column("A_ID");
  }
}

and db configuration

Fluently.Configure().Database(...).Mappings(m =>
  m.FluentMappings.Add<BMap>()).
   ExposeConfiguration(BuildSchema).BuildSessionFactory();

Configuration is done only with BMap as unfortunately table A already exists inside database and it's data can't be changed (it's because this application is using parts of bigger database and those parts are not to be modified). So this schema generates standard exception

An association from the table [B] refers to an unmapped class: A

Is there any possibility somehow specify inside my configuration that A already exists in the db? If not what are your suggestions to solve this issue in a gentle way? Of course I can prepare *.sql files to manually create table B but this is not a solution (just pushing away problem as it will surely come back during further development). Also creating separate db and importing data from the first one could work but unfortunately there is a requirement to store data inside one (and only one) db.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-09-26 19:28:58

您必须映射您将使用的所有实体;它们对应的表是否存在于数据库中是无关紧要的。

只有当您进行模式生成时,它才有意义,但您所要做的就是生成更新脚本而不是创建脚本。给定数据库方言和连接,NHibernate 将生成脚本来创建缺失的表并在必要时更新现有表(听起来在本例中并非如此)。

如果现有表的名称与实体的类型(默认名称)不匹配,您需要通过调用以下命令在映射中指定:

Table("ATable");

You have to map all the entities you will use; whether or not their corresponding tables are present in the database is irrelevant.

Only when you are doing schema generation is it going to matter, but all you have to do is generate update scripts instead of create scripts. Given the database dialect and a connection, NHibernate will generate scripts to create missing tables and update existing tables, if necessary (which it sounds like it isn't in this case).

If the existing table's name does not match the type of the entity (the default name), you'll need to specify that in your mapping by calling:

Table("ATable");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文