I just started to make EntityTypeConfiguration class and did following
public class Xyz
{
public int PlaceId { get; set; }
public string Name { get; set; }
public DbGeography Location { get; set; }
public int HumanTypeId { get; set; }
public int AddressId { get; set; }
}
and in EntityTypeConfiguration class
public sealed class XyzConfiguration:EntityTypeConfiguration<Xyz>
{
public XyzConfiguration()
{
ToTable("Place", "dbo");
HasKey(p => p.PlaceId);
Property(p => p.PlaceId)
.HasColumnName("PlaceId")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.Name);
Property(p => p.Location). ;
Property(p => p.HumanTypeId);
Property(p => p.AddressId);
}
}
Now how to set DbGeography
and foreign key columns HumanTypeId , AddressId
?
Thanks in advance
To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship. I mean to say, define the foreign key table.
You can then configure foreign key properties by using the HasForeignKey method. This method takes a lambda expression that represents the property to be used as the foreign key.
The [ForeignKey(name)] attribute can be applied in three ways: [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.
It depends on what you're going to do with the columns. If you have foreign key columns like AddressId
, you probably have some Address
entities that you want to relate to your Xyz
entities. You need to decide how the entites relate to each other, and configure the mapping you want between them.
You will need a navigation property either in your Address
class, or your Xyz
class, otherwise there isn't anything to bind the foreign key to, and your foreign ID columns would just be treated as normal columns (which is fine, if that's what you want).
So, if your were to add a navigation property to your Xyz
entity
public class Xyz
{
// Your code
public int AddressId { get; set; }
public virtual Address MyAddress { get; set; }
}
// Your Address class
public class Address
{
public int ID;
}
You could configure the mapping by doing something along these lines (it will vary depending on the relationship:
public sealed class XyzConfiguration : EntityTypeConfiguration<Xyz>
{
public XyzConfiguration()
{
// Your code.
this.HasOptional(x => x.MyAddress) // Your Xyz has an optional Address
.WithMany() // Address may be owned by many Xyz objects
.HasForeignKey(x => x.AddressId); // Use this foreign key.
}
}
I haven't tried using spatial types and EF, but I'd start here: http://msdn.microsoft.com/en-us/data/hh859721.aspx
There's a wealth of information on mapping configurations on the getting started with EF pages: http://msdn.microsoft.com/en-us/data/ee712907 try "Fluent API - Configuring/Mapping Properties & Types"
There's also a slightly abridged explanation of the different association types here: Code First: Independent associations vs. Foreign key associations?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With