Using SQL Server json properties with code first EF

Viewed 6557

I want to store details about companies and the cars manufactured by them. Ideally this would include 2 tables: Companies and Cars with a foreign key reference.

But instead I want to use the JSON and NoSQL features of SQL Server 2016. The table structure would be a table Companies and a JSON column in the table with all the information about cars.

To achieve this through code first EF, here are the classes I have created:

public class Company
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime dtEstablished { get; set; }
    public List<Car> CarsManufactured { get; set; }
}

and

public class Car
{
    [Key]
    public string Name { get; set; }
    public string Model { get; set; }
    public DateTime MfgDate { get; set; }
    public string Type { get; set; }
}

and added this property to the ApplicationDbContext class

public DbSet<Company> Companies { get; set; }

Then after applying migration, instead of what I wanted, there are 2 tables in the database with foreign key reference just as discussed.

1 Answers
Related