locked
Many-To-Many mappings and key based access RRS feed

  • Question

  • Hi,

    i know that i can have two properties on a entity if i have a many-to-one mapping, for example

    class User
    {
    ...
    public virtual Company {get; set; }
    public Guid CompanyId {get; set; }
    ...
    }
    

    which is easy as i have defined the CompanyId as table field anyway.

     

    But is such a thing also possible with many-to-many mappings too? If i have a entity like the following:

    class TestLoc
    {
    	...
    	public virtual ICollection<Category> TestCategories { get; set; }
    	...
    }
    
    

    with a mapping like this

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    	modelBuilder.Entity<TestLoc>()
    		.HasMany(x => x.TestCategories)
    		.WithMany()
    		.Map(m =>
    			{
    				m.ToTable("TestLocTestCat");
    				m.MapLeftKey("TestLocId");
    				m.MapRightKey("TestCatId");
    			});
    }
    

     

    Is it possible that i can access the many-to-many mapping using some Key only method - something like a ICollection<Guid> in paralell with the ICollection<Category> as is need to interact (add, remove) with it sometimes by key only (where loading the whole Category instance is superfluous).

     

    Thanks

    Thursday, June 2, 2011 1:47 AM

Answers