0

Using the following JSON object I want to create models in EF and their navigation properties also need model.Builder Entity

Also need information on one to many relation using the json specified below and using migrations create database tables.

[{

"Title": "AC",
"IconClass": "ac",
"Departments": "Housekeeping,Maintenance",
"Status": "NotInOrder",
"Parts": [{
    "Title": "Power",
    "IconClass": "power",
    "Departments": "Housekeeping,Maintenance",
    "Status": "NotInOrder",
    "DependentUpon": "",
    "Id": null
}, {
    "Title": "Remote",
    "IconClass": "remote",
    "Departments": "Housekeeping,Maintenance",
    "Status": "CleanedMaintained",
    "DependentUpon": "Power",
    "Id": null
}, {
    "Title": "Cooling",
    "IconClass": "cooling",
    "Departments": "Housekeeping,Maintenance",
    "Status": "CleanedMaintained",
    "DependentUpon": "Remote",
    "Id": null
}],
},
{

"Title": "TV",
"IconClass": "tv",
"Departments": "Housekeeping,Maintenance",
"Status": "CleanedMaintained",
"Parts": [{
    "Title": "Power",
    "IconClass": "power",
    "Departments": "Housekeeping,Maintenance",
    "Status": "CleanedMaintained",
    "DependentUpon": "",
    "Id": null
}, 
{
    "Title": "TV - Remote",

    "IconClass": "remote",
    "Departments": "Housekeeping,Maintenance",
    "Status": "CleanedMaintained",
    "DependentUpon": "Power",
    "Id": null
}]
]

1 Answer 1

1

According to your JSON object, your Entity Framework model classes should be as follows:

public class Product
{
    [Key]
    public int ProductId {get; set;}
    public string Title { get; set; }
    public string IconClass { get; set; }
    public string Departments { get; set; }
    public string Status { get; set; }

    public ICollection<Part> Parts { get; set; }
}

public class Part
{
    [Key]
    public string Id { get; set; }
    [ForeignKey("Product")]
    public int ProductId {get; set;}
    public string Title { get; set; }
    public string IconClass { get; set; }
    public string Departments { get; set; }
    public string Status { get; set; }
    public string DependentUpon { get; set; }

    public Product Product {get; set;}

}

Then the DbContext:

public class YourDbContext : DbContext
{
    public YourDbContext () : base("name=DefaultConnection")
    {
    }

    public static YourDbContext  Create()
    {
        return new YourDbContext ();
    }

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

    public DbSet<Part> Parts { get; set; }

}
Sign up to request clarification or add additional context in comments.

9 Comments

public class Product { [StringLength(100)] public string Title { get; set; } [StringLength(100)] public string IconClass { get; set; } public virtual ICollection<Department> Departments { get; set; } [JsonConverter(typeof(StringEnumConverter))] public RoomSubEntityPartStatus Status { get; set; } public virtual ICollection<RoomSubPart> RoomSubParts { get; set; } [StringLength(100)] public string DependentUpon { get; set; } }
@Priyanka look at my updated answer! This will be generalized solution. Now you can change things accordingly!
Yeah, but I have an exception to the changes I made, Introducing FOREIGN KEY constraint 'FK_dbo.RoomParts_dbo.RoomEntities_RoomEntityId' on table 'RoomSubParts' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Here RoomParts : Parts, RoomSubEntities : Products
@Priyanka Share classes modifying the question
Ok give me half an hour
|

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.