How do I get fluent nhibernate to save "null" to the db for uninitialized variables - fluent-nhibernate

I have a class like this:
public class test
{
public virtual int Id { get; private set; }
public virtual char testType { get; set; }
public virtual char testType2 { get; set; }
}
When I create this, but don't specificy testType2, I want the value to be "null" in the database. Instead, the value is an empty char. One way that I found to force this to be null is to use char? for the type. However, this apparently doesn't work for strings (doesn't compile), and I really don't want to mess with nullable types. Is there any way to get fluent nhibernate to always save uninitialized variables as "null"?
Edit: uninitialized string variables are saved as "null" in the db. I still need a solution for char and int.

public class test
{
public virtual int? Id { get; private set; }
public virtual DateTime? SomeDate { get; private set; }
public virtual char? testType { get; set; }
public virtual char? testType2 { get; set; }
}
All you need to do is to add a ? after the type.

Related

How can I map my sorting to SQL correctly?

I have the following class:
public class ReturnDto
{
[Column("PARC_NUM")]
public int ParcelNumber { get; set; }
~[Column("CUSTOM_TAXE")] or [Column("AUTOMATIC_TAXE")]~
public decimal Taxe { get; set; }
}
I'm working in this Api using Dapper/Dommel and that class is supposed to be filled with the values from the data base. The problems is that it doesn't work for the second property (the "Taxe" one) because, as follows, the Query have two possible columns to take the value from:
...
ag.PARC_NUM AS ,
case when pp.IDT_MODALID = 3 then ag.CUSTOM_TAXE else ag.AUTOMATIC_TAXE end AS Taxe,
...
I tried setting the column name as Taxe (because that's how I name it, after all), but it just doesn't get mapped correctly. Actually, it just throws an exception poiting that such thing doesn't exist.
I am missing some standard setting or it just can't be done that way?
Try assign one of the valid column name
case when pp.IDT_MODALID = 3
then ag.CUSTOM_TAXE
else ag.AUTOMATIC_TAXE
end AS AUTOMATIC_TAXE ,
or add a field in the class
public class ReturnDto
{
[Column("PARC_NUM")]
public int ParcelNumber { get; set; }
~[Column("CUSTOM_TAXE")] or [Column("AUTOMATIC_TAXE")]~
public decimal Taxe { get; set; }
[Column("TAXE")]
public decimal Taxe { get; set; }
}

WCF error parameter date equals "01/01/0001"

I am currently sending from SoapUI for my tests the following request:
<caja:GenerarObligacion>
<caja:Obligacion>
<obl:BenCheq>TALLERES ARROYO</obl:BenCheq>
<obl:CodMoneda>S/.</obl:CodMoneda>
<obl:CodProd>VM14</obl:CodProd>
<obl:CodRamo>AUTO</obl:CodRamo>
<obl:CodUsuario>ACSELP</obl:CodUsuario>
<obl:DptoEmi>000101</obl:DptoEmi>
<obl:FecFinVig>2024-02-28T12:00:01</obl:FecFinVig>
<obl:FecIniVig>2025-02-28T12:00:01</obl:FecIniVig>
<obl:FecMod>2026-07-17T13:02:05</obl:FecMod>
<obl:FecNotif>2026-07-17T13:02:05</obl:FecNotif>
<obl:FecOcurr>2025-01-28T08:41:53</obl:FecOcurr>
<obl:FecRecepDocPago>2025-01-28T00:00:00</obl:FecRecepDocPago>
<obl:IdePol>25000293961</obl:IdePol>
<obl:IdeSin>1500013900</obl:IdeSin>
<obl:IndCheque>S</obl:IndCheque>
<obl:ItemOblig>
<fac:BEDetalleObligacion>
<fac:CodCpto>DSCTOS</fac:CodCpto>
<fac:ItemDoc/>
<fac:FecFact>2025-01-28T00:00:00</fac:FecFact>
<fac:MtoCptoEgre>600.0</fac:MtoCptoEgre>
<fac:NroFact>444444444</fac:NroFact>
<fac:NumCpto>1</fac:NumCpto>
<fac:SerieFact>f045</fac:SerieFact>
<fac:TipoRegCompra>01</fac:TipoRegCompra>
</fac:BEDetalleObligacion>
<fac:BEDetalleObligacion>
<fac:CodCpto>ISLV</fac:CodCpto>
<fac:ItemDoc/>
<fac:FecFact>2025-01-28T00:00:00</fac:FecFact>
<fac:MtoCptoEgre>108.0</fac:MtoCptoEgre>
<fac:NroFact>444444444</fac:NroFact>
<fac:NumCpto>2</fac:NumCpto>
<fac:SerieFact>f045</fac:SerieFact>
<fac:TipoRegCompra>01</fac:TipoRegCompra>
</fac:BEDetalleObligacion>
</obl:ItemOblig>
<obl:MtoAprob>708.0</obl:MtoAprob>
<obl:NumAprob>18000493</obl:NumAprob>
<obl:NumPol>2500029396</obl:NumPol>
<obl:StsOblig>VAL</obl:StsOblig>
<obl:StsSin>ACT</obl:StsSin>
<obl:TasaCambio>1.0</obl:TasaCambio>
<obl:TipoAprob>P</obl:TipoAprob>
<obl:TipoBenef>TALR</obl:TipoBenef>
<obl:TipoDocPago>CHQ</obl:TipoDocPago>
</caja:Obligacion>
<caja:Tercero>
<ter:BETercero>
<ter:NumId>650606871</ter:NumId>
<ter:TipoId>J</ter:TipoId>
</ter:BETercero>
</caja:Tercero>
</caja:GenerarObligacion>
All the data arrives well except for the "FecFact" field, as seen in the request this field if it has a date but when arriving at the visual debug the following value "01/01/0001" is displayed:
In spite of sending a value to the FecFact field, it is set with the date that is displayed in the image "01/01/0001" as if the field were arriving null. This is the declaration of my entity.
[DataMember]
public String CodCtpo { get; set; }
[DataMember]
public DateTime FecFact { get; set; }
[DataMember] //IndIGV
public String IndnoGravado { get; set; }
[DataMember] //MtoCptoAprob
public Decimal MtoCptoEgre { get; set; }
[DataMember] // NumCpto - ?uso? NUMDETOBLIG
public Int32 NumCpto { get; set; }
[DataMember]
public String NroFact { get; set; }
[DataMember]
public String SerieFact { get; set; }
[DataMember]
public String TipoRegCompra { get; set; } //Funcional como OB
[DataMember]
public String CodSvcio { get; set; }
DateTime is initialized by default by DateTime.MinValue, which is actually 1/1/0001 12:00:00 AM exactly same as what you are getting.
You should declare nullable field if want null Date like:
DateTime? dt = null;
OR
Compare DateTime variables to DateTime.MinValue & set corresponding value in database query as null or in UI variable as null.
According to your description, I have restored your problem successfully,
The only thing you need to do is try to assign the NameSpace attribute to the DataContract Class.
[DataContract(Namespace ="mydomain")]
public class Product
{
[DataMember]
public int ID { get; set; }
[DataMember]
public DateTime dateTime { get; set; }
}
I hope this information is useful.

Parameter xxx of domain operation entry xxx must be one of the predefined serializable types

I get this webservice error sometimes on a SL5 + EF + WCF app.
"Parameter 'role' of domain operation entry 'AddUserPresentationModelToRole' must be one of the predefined serializable types."
here is a similar error, however his solution doesn't work for me.
I have the codegenned DomainService which surfaces the database entities to my client:
[EnableClientAccess()]
public partial class ClientAppDomainService : LinqToEntitiesDomainService<ClientAppUserEntitlementReviewEntities>
{
public IQueryable<Account> GetAccounts()
{
return this.ObjectContext.Accounts;
}
//..etc...
and my custom service which is surfacing a Presentation model, and db entities.
[EnableClientAccess]
[LinqToEntitiesDomainServiceDescriptionProvider(typeof(ClientAppUserEntitlementReviewEntities))]
public class UserColourService : DomainService
{
[Update(UsingCustomMethod = true)]
public void AddUserPresentationModelToRole(UserPresentationModel userPM, Role role, Reviewer reviewer)
{
...
}
public IDictionary<long, byte> GetColourStatesOfUsers(IEnumerable<RBSUser> listOfUsers, string adLogin)
{
//....
}
}
and the PresentationModel:
public class UserPresentationModel
{
[Key]
public long UserID { get; set; }
public byte UserStatusColour { get; set; }
public string MessageText { get; set; }
[Include]
[Association("asdf", "UserID", "UserID")]
public EntityCollection<Account> Accounts { get; set; }
public DateTime AddedDate { get; set; }
public Nullable<long> CostCentreID { get; set; }
public DateTime? DeletedDate { get; set; }
public string EmailAddress { get; set; }
public long EmployeeID { get; set; }
public string FirstName { get; set; }
public Nullable<bool> IsLeaver { get; set; }
public string LastName { get; set; }
public DateTime LastSeenDate { get; set; }
public string LoginDomain { get; set; }
public string LoginName { get; set; }
public byte WorldBuilderStatusID { get; set; }
}
Also cannot get the solution to reliably fail. It seems whenever I change the service slightly ie make it recompile, everything works.
RIAServices unsupported types on hand-built DomainService - seems to be saying the same thing, that decorating the hand built services with the LinqToEntitiesDomainServiceDescriptionProvider should work.
Possible answer here will post back here too with results.
From Colin Blair:
I am a bit surprised it ever works, I don't think I have seen anyone trying to pass additional entiities into a named update before. It might be a bug in RIA Services that it is working at all. What are you trying to accomplish?
Side note, you have a memory leak with your ObjectContext since it is not getting disposed of correctly. Is there a reason you aren't using the LinqToEntitiesDomainSerivce? It would take care of managing the ObjectContext's lifetime for you.
Results:
1) This makes sense. Have refactored out to more sensible parameters now (ints / strings), and all working.
2) Have brought together my 3 separate services into 1 service, which is using the LinqToEntitiesDomainSerivce. The reason I'd split it out before was the assumption that having a CustomUpdate with a PresentationModel didn't work.. and I had to inherit off DomainService instead. I got around this by making a method:
// need this to avoid compile errors for AddUserPresentationModelToRole.. should never be called
public IQueryable<UserPresentationModel> GetUserPresentationModel()
{
return null;
}

DbDataController in MVC4 UpdateEntity failing

I have a datamodel like
ModelA which contains a Collection.
ModelB contains a Collection as a backreference. That is failing because of cyclic references if I query with Include("ModelB"). Not good but I solved that via setting ModelB.List=null for each element.
The problem now is submitting a changed ModelA tree: I am adding ModelB-entities to ModelA.ModelB[]. Now the UpdateEntity function is complaining the it could not add elements of type ModelB which are declared static. The JSON deserializer is creating static arrays.
How is it possible with the combination of upshot/MVC4 to submit datamodels which are not completely flat? As it is not possible right now to create your own DTO objects where you might figure out something I am stuck now...
After investigating the error a bit better, I think the problem is the cyclic backreference:
The ModelA->ModelB->ModelA is breaking the storage of the data. "Could not add data of type ModelA to type ModelB".
As I mentioned the backreference was set to Null because the cyclic ref serialisation problem...
I hope the will be some easier way on doing more manually with DTO objects where I have mroe control.
Please see also: MVC 4, Upshot entities cyclic references for the beginning of the journey...
To solve the cyclic backreference, you can use the ignoreDataMember attribute:
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public virtual ICollection<Delivery> Deliveries { get; set; }
}
public class Delivery
{
[Key]
public int DeliveryId { get; set; }
public string Description { get; set; }
public bool IsDelivered { get; set; }
[IgnoreDataMember]
public virtual Customer Customer { get; set; }
public virtual int CustomerId { get; set; }
}
I posted a working solution to your problem in a different question: https://stackoverflow.com/a/10010695/1226140

EditorTemplates > bool - bool.cshtml, DateTime? - DateTime?.cshtml (fail)

Without using "UIHint" how to create .cshtml for certain types of data.
Examples
These are already working!
[DataType(DataType.Url)]
public string Link { get; set; }
will be used
Url.cshtml
[DataType(DataType.MultilineText)]
public string Descrition { get; set; }
will be used
MultilineText.cshtml
These, I am in doubt about how
public IEnumerable<SelectListItem> TypesList { get; set; }
will be used
???????????.cshtml
public DateTime? DateUpdated { get; set; }
will be used
???????????.cshtml
public int? Order { get; set; }
will be used
???????????.cshtml
Questions
In short, do not want to be putting in my ViewModel attributes, like that would work exactly like "Url" and "Multiline"
What should be the file name .cshtml that will be used to int?, IEnumerable<xx> or IEnumerable<string>
Remembering that you can not create files with "<", ">" or "?"
By convention (if you don't specify any template name):
public IEnumerable<SelectListItem> TypesList { get; set; } will use SelectListItem.cshtml (not that since you have an IEnumerable<T> the editor template will be rendered for each element of the collection and will be called T.cshtml)
public DateTime? DateUpdated { get; set; } will use DateTime.cshtml
public int? Order { get; set; } will use Int32.cshtml
...
You could also specify a template name in the view:
#Html.EditorFor(x => x.TypesList, "MyTemplate.cshtml")
Assuming TypesList is an IEnumerable<T> then MyTemplate.cshtml will be strongly typed to IEnumerable<T> and not to T.