开发者

Linq to SQL foreign key mapping

开发者 https://www.devze.com 2023-02-04 15:08 出处:网络
I\'m trying to do a basic lnq to sql foreign key mapping using attributes. This should be really simple but I\'m having a hard time finding decent info on it on the net. Where am I going wrong?

I'm trying to do a basic lnq to sql foreign key mapping using attributes. This should be really simple but I'm having a hard time finding decent info on it on the net. Where am I going wrong?

Say for example you have a class named User with UserId, FirstName, LastName, Location on it. Location is an object called Location

Location class has LocationId, StreetNum, Name, Suburb

How do I map that with Linq to Sql?

Here's what I'm trying

 [Column]
    public int LocationId { get; set; }

    private EntityRef<Location> _location;
    [Required(ErrorMessage = "Please enter your suburb")]
    [System.Data.Linq.Mapping.Association(Storage = "_location", ThisKey = "LocationId", IsForeignKey = true)]
    public Location Location
    {
        get { return this._location.Entity; }
        set { this._location.Entity = value;
            LocationId = value.LocationId;
        }
    }

I'm getting this error: The null value cannot be assigned to a member with type S开发者_如何转开发ystem.Double which is a non-nullable value type.

Can anyone help?


Probably, somewhere you've used double datatype (StreetNum, maybe). In the database corresponding column is marked with NULL flag. Try to use double? or Nullable<double> datatype for your properties.

0

精彩评论

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