Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate mapping an entity without a primary key

My Entity has no primary key for some reasons:

public partial class VehicleLocation
{
    public virtual string UserCode { get; set; }
    public virtual string DateTime { get; set; }
    public virtual string Device { get; set; }
    public virtual string Gps { get; set; }
    public virtual string GpsDateTime { get; set; }
    public virtual double Speed { get; set; }
}

The mapping:

class VehicleLoactionMap : ClassMap<VehicleLocation> 
{
    public VehicleLoactionMap()
    {
        Table("VEHICLE_LOCATION");
        LazyLoad();
        Map(x => x.UserCode).Column("USER_CODE");
        Map(x => x.DateTime).Column("DATE_TIME");
        Map(x => x.Device).Column("DEVICE");
        Map(x => x.Gps).Column("GPS");
        Map(x => x.GpsDateTime).Column("GPS_DATE_TIME");
        Map(x => x.Speed).Column("SPEED");
    }
}

I got this error:

The entity 'VehicleLocation' doesn't have an Id mapped...

How can I map my entity without using a primary key?

like image 605
Stacked Avatar asked Apr 04 '13 09:04

Stacked


1 Answers

I had the same issue a while back, but after a lot of searching I found that it is not possible to have entities without an identifier field to be mapped in nHibernate.

But you can do this by having composite keys instead of unique identifiers. Although I haven't tried it myself.

Here is an answer to a similar question that might help you.

like image 129
mridula Avatar answered Sep 28 '22 07:09

mridula