Reputation: 131
I have these tables :
public partial class user
{
public string Id { get; set; }
public string name{ get; set; }
}
and
public partial class book
{
public string book-id{ get; set; }
public string book_name{ get; set; }
}
and I'm looking to create a table to hold the user(id) and book (book_id) only to ignore the duplication .
For example: suppose the new table name is Test
:
id ->1
book_id->1
id ->2
book_id->2
if I send again this values :
id ->1
book_id->1
it should not add them to database, so how to define that? What should the new class (Test) property / constraint be? And thanks
Upvotes: 1
Views: 591
Reputation: 23190
Just create a new class Test
like this :
public class Test
{
[Key(Order=0)]
public int UserId { get; set; }
[Key(Order=1)]
public int BookId { get; set; }
public User User { get; set; }
public Book Book { get; set; }
}
By doing that, EF will create a table named Test
in your database. That table will not allow duplication because we use composite key (UserId, BookId).
After that you need to add navigational propertiy public ICollection<Test> Tests { get; set; }
in Book
and User
.
Upvotes: 1