开发者

Problems with inserting the record. Nhibernate

Sorry for bad English, I'm bad at it interprets.

I have a couple of entities: directories and domains. References in the key fields (Id) must not beat an auto-generated, ie I should write them himself.

Here's the code entities.

 /// <summary>
///   Вредитель. [KEN]
/// </summary>
public class Pest
{
    public virtual long Id {get; set;}
    public virtaul string Value {get; set;}
    public virtual string Remark {get; set;}   
}

public class Damage
{
    public virtual long Id {get; set;}        

    public virtual DamageType DamageType { get; set; }

    public virtual Int16 DamageYear { get; set; }

    public virtual Pest FirstPest { get; set; }

    public virtual byte FisrtDamageExtent { get; set; }

}

I used automapping fluentNHibernate.

That overlap is used to handbook.

 public class PestMap : IAutoMappingOverride<Pest>
{
    #region IAutoMappingOverride<Pest> Members

    public void Override( AutoMapping<Pest> autoMapping )
    {
        autoMapping.Id( x => x.Id, "Id" ).GeneratedBy.Foreign();
    }

    #endregion
}

while maintaining the entity instance in the DB

session.Save(
                     new Pest(103)
                     {
                             Id = 103,
                             Value = "value3",
                             Remarks = "Remark3"
                     } );

get an error - Unable to resolve property: id. Tell me please how to solve the problem.

edit

to Cole W is a code generation model:

public class ModelGenerator
{
    public AutoPersistenceModel Generate()
    {
        var automap = new AutoPersistenceModel();

        const string mappingsHbmFolder = @"..\..\Mappings\hbm";
        if (!Directory.Exists(mappingsHbmFolder))
        {
            Directory.CreateDirectory(mappingsHbmFolder);
        }

        automap.Conventions.AddFromAssemblyOf<ModelGenerator>();
        automap.UseOverridesFromAssemblyOf<ModelGenerator>();
        automap.AddEntityAssembly(Assembly.GetAssembly(typeof(Activity)))
            .Where(x => x.Namespace.Contains("Entiti开发者_开发问答es"))
            .IgnoreBase(typeof(HandbookEntity<>))
            .IgnoreBase(typeof(HandbookEntity))
            .IgnoreBase(typeof(Entity<>))
            .IgnoreBase(typeof(Entity))
            .IgnoreBase(typeof(EntityWithCoppice))
            .IgnoreBase(typeof(EntityWithNumber))
            .WriteMappingsTo(mappingsHbmFolder);
        return automap;
    }
}

run programm

private static Configuration CreateSessionFactory()
{
    var modelGenerator = new ModelGenerator();
    return Fluently.Configure()
            .Database(
                      MsSqlConfiguration
                              .MsSql2008
                              .ConnectionString( x => x
                                                              .Server( @"crookpc\sqlexpress" )
                                                              .Database( "b1" )
                                                              .TrustedConnection() )
                              .UseReflectionOptimizer() )
            .Mappings( m => m.AutoMappings.Add( modelGenerator.Generate() ) )
            .ExposeConfiguration( BuildSchema )
            .BuildConfiguration();
}

private static void BuildSchema( Configuration config )
{
    new SchemaExport( config )
            .SetOutputFile( @"db.sql" )
            .Create( false, true );
}

private static void Main( string[] args )
{
    var sessionFactory = CreateSessionFactory().BuildSessionFactory();
        using ( var tx = session.BeginTransaction() )
        {
              session.Save(
                 new Pest(103)
                 {
                         Id = 103,
                         Value = "value3",
                         Remarks = "Remark3"
                 } );
            tx.Commit();
        }


    Console.WriteLine( "Press any key..." );
    Console.ReadKey();
}

}

do you have in mind?


You probably don't want .GeneratedBy.Foreign(), which means that the ID is generated by the other side of 1:1 relationship. If you're just assigning the ID yourself, you should use .GeneratedBy.Assigned().


I would make sure that you are loading your overrides whenever you create your session and auto-mappings. You should probably post that code as well. This is the key line I'm talking about:

AutoPersistenceModel.UseOverridesFromAssemblyOf<PestMap>()

Edit:
More information on this can be found here. Specifically in the "Overrides" section.

Edit2:
Also make sure it's the right entity you are looking at. Is it complaining about the id in Pest or Damage? You may want to post the actual error message.

If this answer helps you mark it as the answer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜