1

In CODE FIRST approach,

How to keep the identity columns as wihtout identity seeds (no auto increment) ?

My entity as follow:

public class Product
{
   public int Id { get; set; }

   ...

}

Also, how to do foreign key here ? I want to add foreign key for below object. what are the properties i have to add into above product class ?

public class ProductType
{

   public int id { get; set; }
   ....

}

Thank You

1 Answer 1

2

By convention, the default setting is

If the type of the primary key property is numeric or GUID it will be configured as an identity column.

You can change that by providing DatabaseGenerated attribute and set it as DatabaseGeneratedOption.None.

To have foreign key from Product to ProductType you only need to provide below collection on ProductType class, it will be automatically discovered by convention, EF will generate foreign key column ProductType_Id in Products table.

public ICollection<Product> Products { get; set; }

However, to have both navigation on both classes you can add below code on Product class.

public int ProductTypeId { get; set; }
public ProductType ProductType { get; set; }

Here is the complete code of the classes.

public class Product
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public int ProductTypeId { get; set; }
    public ProductType ProductType { get; set; }
}
public class ProductType
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public ICollection<Product> Products { get; set; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

So, Is it like- To add FK into ProductType table, I hav to add ICollection<Product> into ProductType table and no need to add any property into Product table ? Another thing: addition of ProductTypeId and Producttype field into Product class is for which reason.. for which relation ?
@user3711357, 1. Yes. 2. With or without ProductTypeId and ProductType properties, the Products will have FK column to ProductTypes, both properties are used in the application so you can easily access ProductType from product instance.
Ok, Thank you for detail, just one last query, In Products , why do we keep two properties - ProductTypeId and ProductType. I mean, both property will be created into table ?
@user3711357, Both of them are simply navigations, from product instance, we can access ProductTypeby product.ProductType, from productType instance we can access all products underneath it by productType.Products, If you don't need to be able to access that way, you don't have to add both properties, having Products collection only already sufficient to create a referential integrity in the database.

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.