Composite Key Mapping

compositeKey.png

The table above is identified with two primary keys. So, the entity mapping will be as the following.


using System;
using PersistentManager.Mapping;
using System.Collections.Generic;
using entities;

//Entity Class
[Entity("Address")]
[CompositeKey(KeyName = "Id, CustomerId", Types = new[] { typeof(Int64), typeof(Int64)})]
public class Address
{
    [Key("Id", AutoKey = true)]    
    public long Id { get; set; }

    [OneToOne(typeof(Customer), RelationColumn = "CustomerId")]
    public Customer Customer { get; set; }

    [Field("Street", false, true)]
    public string Street { get; set; }

    [Field("HouseNo", false, true)]
    public string HouseNo { get; set; }

    [Field("PostCode", false, true)]
    public string PostCode { get; set; }

    [Field("County", false, true)]
    public virtual string Country { get; set; }


	public Address()
	{

	}
}


A composite Entity's relationship with another Entity
How do we ensure a relationship with another entity? The code below show how a Customer class has a One-To-One relationship with the Address class. Note the mapping very well, if you are to use Composite Mapping.


        [OneToOne(typeof(Address), RelationColumn = "AddressId, Id", Cascade = Cascade.ALL)]
        public Address Address
        {
            get{return address;}
            set{address = value;}
        }


Note the code above supplies the AddressId and Id. These names are from the customer table, and they follow the sequence at which the address entity describes its Composite Mapping.

Last edited Sep 28, 2008 at 11:12 AM by ahmedsalako, version 2

Comments

No comments yet.