I have used CodeFirst approach with FluentNhibernate and automapping.
public class DBUser
{
public virtual IList<DBComment> Comments { get; set; }
public virtual long Id { get; set;
}
public class DBComment
{
public virtual int Id { get; set; }
}
var mapping = AutoMap.AssemblyOf<DBModel.DBUser>()
.Where(x => x.Namespace == "DBModel")
.Conventions.Add<CascadeConvention>()
.Conventions.Add<PrimaryKeyConvention>()
;
public class CascadeConvention : IReferenceConvention, IHasManyConvention, IHasManyToManyConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.Cascade.All();
instance.LazyLoad();
//instance.Not.LazyLoad();
}
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Cascade.All();
instance.LazyLoad();
}
public void Apply(IManyToManyCollectionInstance instance)
{
instance.Cascade.All();
instance.LazyLoad();
}
}
This code generates following DB:
CREATE TABLE "DBComment" (Id integer primary key autoincrement, DBUser_id BIGINT, constraint FKFED204719FFB426D foreign key (DBUser_id) references "DBUser")
CREATE TABLE "DBUser" (Id integer primary key autoincrement)
Task is following: I have record DBUser in my DB(say it's id is '28') which already has some comments. And I want to add more comments to this user. Ofc, I could use the following code to update it:
var dbUser = session.Load<DBUser>("28");
dbUser.Comments.Add(comment);
session.Update(dbUser);
But it works slow and doing unnecessary requests. Are there any other ways how I could add comment to existing user? May be not using NHibernate but just by SQL request.
Aucun commentaire:
Enregistrer un commentaire