English 中文(简体)
Castle ActiveRecord: one-to-one
原标题:

While playing around with one-to-one associations in castle activerecord I stumbled upon the following problem:

I m trying to model a one-to-one relationship (user-userprofile in this case). I already learned that this may not be a best practice, but let s ignore that for a moment (I m still trying to understand what s going on).

[ActiveRecord]
public class TestUser : ActiveRecordBase<TestUser>
{
    [PrimaryKey(PrimaryKeyType.GuidComb)]
    public Guid Id { get; set; }


}

[ActiveRecord]
public class TestUserProfile : ActiveRecordBase<TestUserProfile>
{
    [PrimaryKey(PrimaryKeyType.GuidComb)]
    public Guid Id { get; set; }

    [OneToOne(Cascade = CascadeEnum.All, Fetch = FetchEnum.Join)]
    public TestUser User { get; set; }
}

I would expect the following code to save a user with profile, yielding the same Id in the database:

    [Test]
    public void save_profile_saves_user()
    {
        var profile = new TestUserProfile
        {
            User = new TestUser()
        };

        profile.Save();

    }

The actual result however is that both objects are saved with a different key. Am I missing something??

最佳回答

I ve found the answer myself. The PrimaryKeyType of the side of the relation where OneToOne is defined should have a PrimaryKey of PrimaryKeyType.Foreign:

[ActiveRecord]
public class TestUserProfile : ActiveRecordBase<TestUserProfile>
{
    [PrimaryKey(PrimaryKeyType.Foreign)]
    public Guid Id { get; set; }

    [OneToOne(Cascade = CascadeEnum.All, Fetch = FetchEnum.Join)]
    public TestUser User { get; set; }

 }

Back to reading the docs more thoroughly...

问题回答

暂无回答




相关问题
Codeigniter WHERE on "AS" field

I have a query where I need to modify the selected data and I want to limit my results of that data. For instance: SELECT table_id, radians( 25 ) AS rad FROM test_table WHERE rad < 5 ORDER BY rad ...

Problem find joined table in rails

I have model represent association rule (Body => Head) def Item has_many :heads has_many :bodies ... end def Rule has_many :heads has_many :bodies ... end def Body belongs_to :item belongs_to :rule ...

FreeTDS Bad token from the server (SQL Server)

Today we had a lot more activity than normal between our Ruby on Rails application and our remote legacy SQL Server 2005 database, and we started getting the error below intermittently. What is is? ...

Castle ActiveRecord: one-to-one

While playing around with one-to-one associations in castle activerecord I stumbled upon the following problem: I m trying to model a one-to-one relationship (user-userprofile in this case). I ...

Sending email updates: model or observer?

I have an Event model, which stores an event feed for each user. I also need to email the updates to users which have enabled email notifications in their profile. From an architectural point of view,...

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

热门标签