开发者

Fluent NHibernate mapping a reference to a column that is not the ID column

I am using Fluent Nhibernate 1.0 with Sharp Architecture 1.0

Currently I am trying to map a reference to a ZipCode class. The current class I am mapping has a ZipCode column, but the zipcode class is much more extensive than what is needed for a basic zipcode, hence the reasoning behind this. (Basically Zipcode class contains lat. and long. UTC time zone etc, all things that are readonly)

This is my mapping

            References<ZipCode>(x => x.ZipCodeRadius, "ZipCode")
            .Column("ZipCode")
            .Cascade.None()
            //.ForeignKey("FK_ZipCode")
            .ReadOnly();

and when I am running my tests, I am getting this error.

Initialization method CountryRepositoryTests.SetUp threw exception. System.Data.SqlClient.S开发者_JAVA技巧qlException: System.Data.SqlClient.SqlException: Column 'ZipCode.ZipCodeID' is not the same data type as referencing column 'Address.ZipCode' in foreign key 'FK8C1490CB2993CD44'. Could not create constraint. See previous errors..

I have tried adding the ForeignKey and the Constrained lambdas, but they seemed to not add anything.

The zipcode tables has an ID, but I do not want to map to that, rather I want to map to the zipcode column of the zipcode table, back to the zipcode column of the address table.

If anyone has any ideas how I can get around this, I would really appreciate it.

Please note as I did above, I can not simply just reference the zipcode table and drop the property on the address because the zipcode table is readonly.

This is the ZipCodeRadius class.

[NotNullNotEmpty, Length(Max = 5)]
public virtual string ZipCodeName { get; set; }
[NotNullNotEmpty, Length(Max = 1)]
public virtual string ZipType { get; set; }
[NotNullNotEmpty, Length(Max = 10)]
public virtual string TimeZone{ get; set; }

public virtual int UTC  { get; set; }

public virtual double Latitude { get; set; }
public virtual double Longitude { get; set; }

public virtual County County { get; set; }

This is the Address class

protected Address() { }

public Address(User UpdateUser)
    : base(UpdateUser)
{
    this.UpdateUserId = UpdateUser.Id.ToString();  
}

//[DomainSignature, NotNullNotEmpty]
//public virtual string Title { get; set; }

[NotNullNotEmpty, Length(Max = 50)]
public virtual string AddressLine1 { get; set; }

[Length(Max = 50)]
public virtual string AddressLine2 { get; set; }

[NotNullNotEmpty, Length(Max = 20)]
public virtual string City { get; set; }

public virtual StateOrProvince State { get; set; }

[NotNullNotEmpty, Length(Max = 10)]
public virtual string ZipCode { get; set;}

[Length(Max = 10)]
public virtual string ZipPlus { get; set; }

public virtual bool IsVerified { get; set; }

public virtual Country Country { get; set; }

public virtual ZipCode ZipCodeRadius { get; set; }

This is the ZipCode table mapping

Table("ZipCode");

Id(x => x.Id, "ZipCodeID");

Map(x => x.ZipCodeName, "ZipCode").AsVarChar(5);
Map(x => x.ZipType, "ZipType").AsVarChar(1);
Map(x => x.TimeZone, "TimeZone").AsVarChar(10);
Map(x => x.UTC, "UTC");
Map(x => x.Latitude, "Latitude");
Map(x => x.Longitude, "Longitude");

ReadOnly();
Cache.ReadOnly();


Apparently HasOne is the proper way to do this.... Even though I thought I tried it, I was missing the PropertyRef...

            HasOne<ZipCode>(x => x.ZipCodeRadius)
            .PropertyRef(x => x.ZipCodeName)
            .ForeignKey("ZipCode");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜