首先,抱歉我的英语不好
我有以下实体
public class User { public virtual int Id { get; set; } public virtual Application Application { get; set; } public User () { Application = new Application (); } }用户映射
public class UserMap : ClassMap<User> { public UserMap () { Table ("Users"); Id (p => p.Id); References (x => x.Application).Cascade.SaveUpdate (); } }应用
public class Application { public virtual int Id { get; set; } public virtual string ApplicationName { get; set; } }ApplicationMap
public ApplicationMap () { Table ("Applications"); Id (x => x.Id); Map (x => x.ApplicationName); }我收到了这个json
{ "Application": 1, }并以这种方式保存对象
var user = new User(); user.Application.Id = Cast.To<int>(userModel.Application); userService.Add(user); userService.Commit();正确记录表“users”中的数据但留空,“ApplicationName”表字段“application”
我认为错误是在这一行(user.Application.Id = Cast.To(userModel.Application);)因为我没有设置“ApplicationName”字段,但如果我将id作为参数,我将需要得到应用程序对象按id,并分配用户对象? 非常感谢你
first of all, sorry for my bad English
I have the following entities
public class User { public virtual int Id { get; set; } public virtual Application Application { get; set; } public User () { Application = new Application (); } }UserMap
public class UserMap : ClassMap<User> { public UserMap () { Table ("Users"); Id (p => p.Id); References (x => x.Application).Cascade.SaveUpdate (); } }Application
public class Application { public virtual int Id { get; set; } public virtual string ApplicationName { get; set; } }ApplicationMap
public ApplicationMap () { Table ("Applications"); Id (x => x.Id); Map (x => x.ApplicationName); }I received this json
{ "Application": 1, }and save the object this way
var user = new User(); user.Application.Id = Cast.To<int>(userModel.Application); userService.Add(user); userService.Commit();Correctly records the data in the table "users" but left blank, the "ApplicationName" table field "application"
I think the error is in this line (user.Application.Id = Cast.To (userModel.Application);) because I did not set "ApplicationName" field but if I get the id as a parameter, I will need to get the application object by id, and assign the user object?. thank you very much
最满意答案
你是对的,从ID到Entity (Applicaton)的转换需要调用数据层及其操作GetById() 。
session.Load<Application>(id)
在我们可以肯定的情况下,传递的应用程序ID存在,NHibernate有一个专门的方法如何将ID转换为其ENTITY。 它是一个Load()方法,它不会命中数据库,只需创建一个带有提供ID的代理,操作就会成功:
var user = new User(); var applicationId = Cast.To<int>(userModel.Application); // behind is session.Load<Aplication>(applicaitonId) var application = applicationService.Load(applicationId); user.Application = application; userService.Add(user);session.Get<Application>(id)
另一种方法是Get()方法,它总是按ID加载实例,即命中数据库。 如果ID与任何ID不匹配,则返回null。 这种情况的优点是,我们甚至可以更改引用的ApplicaitonName (如果应用程序存在)
var user = new User(); var applicationId = Cast.To<int>(userModel.Application); // behind is session.Get<Aplication>(applicaitonId) var application = applicationService.GetById(applicationId); if(application !== null) { application.ApplicationName = ... // here we can even change that; user.Application = application; }新的级联。
如果我们收到全新的物品
{ "Application": { "ApplicaitonName" : ... } }我们可以创建一个,并且由于上面的Cascading设置,它也可以工作
user.Application = new Appliation { ApplicationName = ..., }You are right, conversion from ID into Entity (Applicaton) will require call to data layer and its operation GetById().
session.Load<Application>(id)
In cases, that we can be sure, that the passed Application ID exists, NHibernate has a dedicated way how to convert ID into its ENTITY. It is a Load() method, which does NOT hit the DB, just creates a proxy with provided ID, and the operation will succeed:
var user = new User(); var applicationId = Cast.To<int>(userModel.Application); // behind is session.Load<Aplication>(applicaitonId) var application = applicationService.Load(applicationId); user.Application = application; userService.Add(user);session.Get<Application>(id)
The alternative is a Get() method, which always loads the instance by ID, i.e. hits the DB. If the ID does not match any ID, null is returned. Advantage in this scenario is, that we can even change the referenced ApplicaitonName (if application exists)
var user = new User(); var applicationId = Cast.To<int>(userModel.Application); // behind is session.Get<Aplication>(applicaitonId) var application = applicationService.GetById(applicationId); if(application !== null) { application.ApplicationName = ... // here we can even change that; user.Application = application; }cascade for new.
In case we would recieve brand new object
{ "Application": { "ApplicaitonName" : ... } }We can create one, and because of Cascading setting above, it will work as well
user.Application = new Appliation { ApplicationName = ..., }更多推荐
发布评论