开发者

Fluent nHibernate, Reverse One-One Property

开发者 https://www.devze.com 2023-02-26 06:29 出处:网络
I thought this would be pretty simple, but it is proving a bit more frustrating than I anticipated. Given a structure similar to this ...

I thought this would be pretty simple, but it is proving a bit more frustrating than I anticipated.

Given a structure similar to this ...

class Template {
  public virtual int Id { get; set; }
  public virtual Attached Attached { get; set; }
}

class Attached {
 public virtual int Id { get; set; }
 public virtual Template Template { get; set; }
}

I want the table structure to look like this.

Table - Template

Id

Table - Attached

Id - TemplateId

So I set up my mapping ...

class TemplateMap : ClassMap<Template> {
  TemplateMap(){
    HasOne(x => x.Attached).Cascade.All();
  Table("Templates");
  }
}
class AttachedMap : ClassMap<Attached> {
  AttachedMap(){
    References(x => x.Template).Cascade.All().Column("TemplateId");
    Table("Attache开发者_如何学运维d");
  }
}

then I create a new Template

var tmpl = new Template {
  Attached = new Attached {
    // ... 
  }
};

session.SaveOrUpdate(tmpl);
transaction.Commit();

But my TemplateId in the Attached table still comes out Null. Any ideas?


I think both sides of the relationship here need to be a one to one. Also you'll need to specify on one side of the relationship that the id is generated foreignly.

Read the following 2 articles if you need more detail:
link1
link2

Edit:
Here is an example in Fluent of what I'm talking about and also what they talked about in link1:
Fluent Example

0

精彩评论

暂无评论...
验证码 换一张
取 消