Search code examples
c#sqlitenhibernatefluent-nhibernate

Update one to many relationship without loading objects by NHibernate


I have used CodeFirst approach with FluentNhibernate and automapping.

namespace DBModel
{
    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; }
        public virtual long CommentId { 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.


Solution

  • Finally, I've found simple solution using sqlite-net client:

    I've created new class:

    [SQLite.Table("DBComment")]
    public class DBCommentLite
    {
        public DBCommentLite(DBModel.DBUser user, DBModel.DBComment comment)
        {
            DBUser_id = user.Id;
            CommentId = comment.CommentId;
        }
    
        [SQLite.PrimaryKey]
        [SQLite.AutoIncrement]
        public int Id { get; set; }
    
        public long CommentId { get; set; }
    
        public long DBUser_id { get; set; }
    }
    

    And use it like:

            var newComments = newUsers.SelectMany(user =>
            {
                return user.Comments.Select(comment => new DBCommentLite(user, comment));
            });
            _InsertAll(newComments);
    
        private void _InsertAll(IEnumerable collection)
        {
            using (var db = new SQLite.SQLiteConnection(_DBName))
            {
                db.InsertAll(collection);
                db.Commit();
            }
        }
    

    That works much faster then NHibernate solution, which I've implemented. And when I get this users by NHibernate next time, I got all comments which they have before, and the new ones which have been added by this code.