开发者

SharpArch.Core.PreconditionException: For better clarity and reliability, Entities with an assigned Id must call Save or Update

开发者 https://www.devze.com 2022-12-24 00:53 出处:网络
When I do a save of an entity with assigned id I get a SharpArch.Core.PreconditionException: For better clarity and reliability,

When I do a save of an entity with assigned id I get a

SharpArch.Core.PreconditionException: For better clarity and reliability, Entities with an assigned Id must call Save or Update

My class is

public class CompanyUserRepository :
RepositoryWithTypedId<CompanyUser, string>, ICompanyUserRepository
{
   public override CompanyUser SaveOrUpdate(CompanyUser entity)
   {
     var user = base.SaveOrUpdate(entity);
     //Do some stuff
     return user;
   }
}

How do I go about saving this entity?

RepositoryWithTypedId does not开发者_开发技巧 expose a Save method

Related question. This tells you the reason, but I haven't found out the Sharp Architecture way to do a Save.


Digging through the NorthWind example in the S#arp repo, I found out the answer.

The Repository class should derive from NHibernateRepositoryWithTypedId

e.g.

public class CustomerRepository : NHibernateRepositoryWithTypedId<Customer, string>, ICustomerRepository
{
    public List<Customer> FindByCountry(string countryName) {
        ICriteria criteria = Session.CreateCriteria(typeof(Customer))
            .Add(Expression.Eq("Country", countryName));


        return criteria.List<Customer>() as List<Customer>;
    }
}
0

精彩评论

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