when I want to send the data to view, I take the invalid column error CreamType_Id
: enter image description here
System.Data.Entity.Core.EntityCommandExecutionException: 'An error occurred while executing the command definition. See the inner exception for details.'
SqlException: Invalid column name 'CreamType_Id'.
I did that by example. but it isn't work, and I don't know how to fix that
my models
public class CreamTypeModel
{
[Key]
public int Id { get; set; }
public string Type { get; set; }
}
public class CreamModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string ImageName { get; set; }
public int? Type_id { get; set; }
public CreamTypeModel CreamType { get; set; }
}
They nothing to do with the public CreamTypeModel CreamType { get; set; }
in example.
my code in database
CREATE TABLE [dbo].[CreamTypeModels] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Type] NVARCHAR (100) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[CreamModels] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (70) NOT NULL,
[Description] NVARCHAR (250) NOT NULL,
[Price] DECIMAL (18, 2) NOT NULL,
[ImageName] NVARCHAR (100) NOT NULL,
[Type_id] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
FOREIGN KEY ([Type_id]) REFERENCES [dbo].[CreamTypeModels] ([Id]) ON DELETE SET NULL
);
repository code
public class CreamRepository : ICreamRepository
{
private CreamEFDbContext context = new CreamEFDbContext();
public IEnumerable<CreamModel> CreamList
{
get { return context.CreamModels.Include(x => x.CreamType); }
}
public IEnumerable<CreamTypeModel> CreamTypeList
{
get { return context.CreamTypeModels; }
}
}
and controller method
[HttpGet]
[Authorize(Roles = "Administrator")]
public PartialViewResult TableCreams()
{
return PartialView(creamManager.CreamList.ToList());
}
I find how to fix that I rename the field public int? Type_id { get; set; }
to public int? CreamTypeModel_id { get; set; }
and in database too. But really interesting why that isn't work ? without this fix?? Have somebody any idea?