开发者

Guid values in Oracle with fluentnhibernate

开发者 https://www.devze.com 2023-04-02 20:24 出处:网络
I\'ve only been using fluent nhibernate a few days and its been going fine until trying to deal with guid values and Oracle. I have read a good few posts on the subject but none that help me solve the

I've only been using fluent nhibernate a few days and its been going fine until trying to deal with guid values and Oracle. I have read a good few posts on the subject but none that help me solve the problem I am seeing.

I am using Oracle 10g express edition.

I have a simple test table in oracle

CREATE TABLE test (Field RAW(16));

I have a simple class and interface for mapping to the table

public class Test : ITest
{
    public virtual Guid 开发者_StackOverflowField { get; set; }
}

public interface ITest
{
    Guid Field { get; set; }
}

Class map is simple

public class TestMap : ClassMap<Test>
{
    public TestMap()
    {
        Id(x => x.Field);
    }
}

I start trying to insert a simple easily recognised guid value

00112233445566778899AABBCCDDEEFF

Heres the code

var test = new Test {Field = new Guid("00112233445566778899AABBCCDDEEFF")};

// test.Field == 00112233445566778899AABBCCDDEEFF here.

session.Save(test);

// after save guid is changed, test.Field == 09a3f4eefebc4cdb8c239f5300edfd82
// this value is different for each run so I pressume nhibernate is assigning
// a value internally.

transaction.Commit();

IQuery query = session.CreateQuery("from Test");
// or
// IQuery query = session.CreateSQLQuery("select * from Test").AddEntity(typeof(Test));
var t in query.List<Test>().Single();

// t.Field == 8ef8a3b10e704e4dae5d9f5300e77098
// this value never changes between runs.

The value actually stored in the database differs each time also, for the run above it was

EEF4A309BCFEDB4C8C239F5300EDFD82

Truly confused....

Any help much appreciated.

EDIT: I always delete data from the table before each test run. Also using ADO directly works no problem.

EDIT: OK, my first problem was that even though I thought I was dropping the data from the table via SQL command line for oracle when I viewed the table via oracle UI it still had data and the first guid was as I should have expected 8ef8a3b10e704e4dae5d9f5300e77098.

Fnhibernate still appears to be altering the guid value on save. it alters it to the value it stores in the database but I'm still not sure why it is doing this or how\if I can control it.


If you intend on assigning the id yourself you will need to use a different id generator than the default which is Guid.comb. You should be using assigned instead. So your mapping would look something like this:

Id(x => x.Field).GeneratedBy.Assigned();

You can read more about id generators in the nhibernate documentation here:

http://www.nhforge.org/doc/nh/en/index.html#mapping-declaration-id-generator

0

精彩评论

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

关注公众号