0
get this json
{
"Name": "Teste",
"ExpirationDate": "/Date(1515703416000-0200)/",
"Program": {
"Id": 1
},
"Resource":{
"Id": 1
},
"InsertDate": "/Date(1515703416000-0200)/",
"InsertUserId": 8,
"LastUpdateDate": null,
"LastUpdateUserId": null
}
and my service is so:
public string CreateAbility(CallDomain.Ability ability)
{
if (ReferenceEquals(ability, null))
{
throw new WcfException(System.Net.HttpStatusCode.BadRequest, ErrorMessage.Resource.EMPTY);
}
else if(!(ability.Program?.Id > 0))
{
throw new WcfException(System.Net.HttpStatusCode.BadRequest, ErrorMessage.Program.ID_REQUIRED);
}
else if (!(ability.Resource?.Id > 0))
{
throw new WcfException(System.Net.HttpStatusCode.BadRequest, ErrorMessage.Resource.ID_REQUIRED);
}
var repository = new AbilityRepository(_user.Id);
ability.IsActive = true;
var repResource = new ResourceRepository(_user.Id);
var dbResource = repResource.Get(ability.Resource.Id);
var repProgram = new ProgramRepository(_user.Id);
var dbProgam = repProgram.Get(ability.Program.Id);
ability.Resource = dbResource;
ability.Program = dbProgam;
_ability = repository.Create(ability);
return CustomJsonSerializer.Serialize(_ability);
}
I really need to search my database Program
and Resource
, before inserting the ability
?
The Resource
and the Program
will already exist in the BD at the time I am inserting the object ability
<?xml version="1.0" encoding="utf-8" ?>
mapping Ability
<class name="Ability" table="Ability">
<id name="Id" column="Id">
<generator class="native" />
</id>
<property name="Name" column="Name" />
<property name="IsActive" column="IsActive" />
<property name="ExpirationDate" column="ExpirationDate" />
<property name="InsertDate" column="InsertDate" />
<property name="InsertUserId" column="InsertUserId" />
<property name="LastUpdateDate" column="LastUpdateDate" />
<property name="LastUpdateUserId" column="LastUpdateUserId" />
<many-to-one name="Program" column="ProgramId" fetch="join" />
<many-to-one name="Resource" column="ResourceId" fetch="join" />
</class>
</hibernate-mapping>
mapping program
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="BipDomain"
namespace="BipDomain.Models.ProgramDomain">
<class name="Program" table="Program">
<id name="Id" column="Id">
<generator class="native"/>
</id>
<property name="Name" column="Name" />
<property name="IsActive" column="IsActive" />
<property name="InsertDate" column="InsertDate" />
<property name="InsertUserId" column="InsertUserId" />
<property name="LastUpdateDate" column="LastUpdateDate" />
<property name="LastUpdateUserId" column="LastUpdateUserId" />
<bag name="Abilities" cascade="All">
<key column="ProgramId" />
<one-to-many class="BipDomain.Models.CallDomain.Ability" />
</bag>
</class>
</hibernate-mapping>
re-source mapping
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="BipDomain"
namespace="BipDomain.Models.ProgramDomain">
<class name="Resource" table="Resource">
<id name="Id" column="Id">
<generator class="native"/>
</id>
<property name="Name" column="Name" />
<property name="IsActive" column="IsActive" />
<property name="InsertDate" column="InsertDate" />
<property name="InsertUserId" column="InsertUserId" />
<property name="LastUpdateDate" column="LastUpdateDate" />
<property name="LastUpdateUserId" column="LastUpdateUserId" />
<bag name="Abilities" cascade="All" >
<key column ="ResourceId" />
<one-to-many class="BipDomain.Models.CallDomain.Ability"/>
</bag>
</class>
</hibernate-mapping>
Probably not, only foreign keys is enough for the Entity to understand that records already exist. However, it is difficult to know without analyzing your mapping/repository.
– Gabriel Coletta
Why don’t you take the test? Comment on the 2 lines and try to insert
– PauloHDSousa
I added the mapping only one detail, not this with Entity but with Nhibernate
– Jhonatan Jorge de Lima