1

using the codefirst approach, I would like to have a CODE_YESNO table where it will have two columns C_CODE(1) and C_DESC(5) both type of string.

then I would like to reference it to my other tables.

for example lets say I have I have USERS and PRODUCTS table

and for USERS table I will have userIsActive (foreign key C_CODE) and for PRODUCTS table I will have productIsOnDiscount (foreign key C_CODE)

I don't want to add or do any modifications on the CODE_YESNO table

so how do I do that with the code first approach?

3
  • Please enrich your question with detailed table, it's difficult for users to understand your table. Commented Jan 26, 2018 at 14:30
  • Start by modeling your classes (tables) in c#. Add fields and references between tables as properties to those classes. When you are ready, then you have your code first model. Commented Jan 26, 2018 at 14:44
  • how do I do that with the code first approach? That's far too broad for a Stack Overflow question. Commented Jan 26, 2018 at 14:50

1 Answer 1

2

Personally, I would use a boolean if it is just Yes/No. But more generally you can implement lookup tables easily.

public class YesNo
{
    [Key]
    public string C_CODE { get; set; }
    public string C_DESC { get; set; }
}

Then your classes reference through navigation properties:

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }

    public string UserIsActiveCode { get; set; }
    [ForeignKey("UserIsActiveCode")]
    public YesNo UserIsActive { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }

    public string ProductIsOnDiscountCode { get; set; }
    [ForeignKey("ProductIsOnDiscountCode")]
    public YesNo ProductIsOnDiscount { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

a lot of love sir <3 thanks to you I'm getting the hang of it:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.