현재 발생하는 문제를 사용하여 Session.Save와 NHibernate에 4를 통해 SQL Server 데이터베이스에 삽입 이런 모습.하지 레코드를 삽입하고 다음과 같은 예외를 생산) (Session.Save로 매핑 별 코드
:활동 (이 클래스와 관련 단순 위해, 내가 삭제 한 여러 ILists)
public class Activity {
public Activity() {
Activityschema = new List<ActivitySchema>();
}
public virtual int ActivityKey { get; set; }
public virtual string Activityname { get; set; }
public virtual string Activitydescription { get; set; }
public virtual DateTime Averageactivitytime { get; set; }
public virtual int Averagenumberpeople { get; set; }
public virtual string Worktype { get; set; }
public virtual bool? Canautocomplete { get; set; }
public virtual IList<ActivitySchema> Activityschema { get; set; }
}
ActivityMap 마지막으로
public class ActivityMap : ClassMapping<Activity> {
public ActivityMap() {
Schema("dbo");
Lazy(true);
Id(x => x.ActivityKey, map => { map.Generator(Generators.Identity); });
Property(x => x.Activityname, map => { map.NotNullable(true); map.Length(50); });
Property(x => x.Activitydescription, map => { map.NotNullable(true); map.Length(100); });
Property(x => x.Averageactivitytime, map =>
{
map.NotNullable(true);
map.Type(NHibernateUtil.Time);
});
Property(x => x.Averagenumberpeople, map => { map.NotNullable(true); map.Precision(10); });
Property(x => x.Worktype, map => { map.NotNullable(true); map.Length(50); });
Property(x => x.Canautocomplete);
Bag(x => x.Activityschema, colmap => { colmap.Key(x => x.Column("ActivityKey")); colmap.Inverse(true); }, map => { map.OneToMany(); });
}
}
, 여기에 내가 가지고 작업 클래스의 단위입니다
public class NHUnitOfWork : IDisposable
{
public static string ConnectingString { get; private set; } = @"data source=nh;initial catalog=db;MultipleActiveResultSets=True;";
protected static Configuration _config;
protected static NHibernate.ISessionFactory _sessionFactory;
public NHibernate.ISession Session { get; private set; }
protected NHibernate.ITransaction Transaction { get; set; }
private const System.Data.IsolationLevel ISOLATION_LEVEL = System.Data.IsolationLevel.ReadUncommitted;
private bool RollBack { get; set; } = false;
public NHUnitOfWork(string databaseConnectionString)
{
if (_config == null)
{
var cfg = new Configuration();
cfg.DataBaseIntegration(db =>
{
db.Driver<NHibernate.Driver.SqlClientDriver>();
db.ConnectionString = @"data source=nh;initial catalog=db;MultipleActiveResultSets=True;";
//db.ConnectionString = databaseConnectionString;
db.Dialect<MsSql2012Dialect>();
db.BatchSize = 500;
})
.AddAssembly(typeof(Activity).Assembly)
.SessionFactory()
.GenerateStatistics();
var mapper = new ModelMapper();
mapper.AddMappings(typeof(ActivityMap).Assembly.GetTypes());
cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
_config = cfg;
_sessionFactory = _config.BuildSessionFactory();
}
Session = _sessionFactory.OpenSession();
Transaction = Session.BeginTransaction(ISOLATION_LEVEL);
RollBack = false;
}
public void Commit()
{
Transaction.Commit();
}
public void Rollback()
{
if (Transaction.IsActive) Transaction.Rollback();
}
public void Dispose()
{
if (RollBack)
{
Transaction.Rollback();
}
else
{
Transaction.Commit();
}
Session.Close();
}
}
이 시점까지는이 구성이 맞다고 생각합니다. 나는 Session.Query를 성공적으로 사용하여 데이터를 읽었으며 문제를 나타내지 않는다.
var activity = new Activity
{
Activityname = "TestActivity",
Activitydescription = "This is a test",
Averagenumberpeople = 1,
Worktype = "Test",
Canautocomplete = false,
Averageactivitytime = new DateTime(1, 1, 1, 0, 55, 55)
};
using (var uow = new NHUnitOfWork(NHUnitOfWork.ConnectingString))
{
uow.Session.Save(activity); // Produces exception here
//This also produces an exception
//uow.Session.Save(activity, Generators.Identity);
}
나는 이것이 내가 ActivityMap의 ID를 매핑하고있어 예상대로 발전기가 작동하지 않는 방법을 함께 할 수있는 뭔가가 생각 : 나는 새 레코드를 추가하기 위해 다음처럼 작성할 때 문제가 온다. 다른 여러 유형으로 변경하려고 시도하고 동일한 예외가 발생하거나 SystemInt32로 변환 할 수 없다는 메시지가 나타납니다. 또한 ID를 길게 변경하고 데이터 형식을 지정하려고 시도했지만 행운은 없습니다. 내가 여기서 뭘 잘못하고있는 것 같니?
게시 한 답변을 보면 ** 간단한 인쇄상의 오류 **로 보이는 것이며 질문 본문에 설명 된 내용과 아무런 관련이 없습니다. –
내 혼란은 주로 나에게 내포하는 null id에 관한 예외에서 기인하는 것이 었습니다. 이것은 설정되지 않은 다른 non-id 속성 중 하나와 반대되는 id 속성의 문제를 의미합니다. 내가 게시 한 답변은 삽입물이 나에게 올바르게 작동하도록 해 주었고, "내가 여기서 뭘 잘못하고있는 것 같니?"라는 내 자신의 질문에 대답합니다. 비록 제가 게시 한 다른 코드의 대부분과 관련이 없더라도 말입니다. – RioC