0

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)
);

CreamTypeModels

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
);

CreamsModels

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?

  • creamManager.CreamList.ToList() --> CreamList is a method. So you should call it like that: creamManager.CreamList(). By the way, what does the inner exception say? provide the message, not a temporary image. – momo Mar 05 '19 at 13:43
  • check the database (not the script) and confirm that the column name is `Id` or `CreamType_Id` in `CreateTypeModels` – Matt.G Mar 05 '19 at 13:51
  • Matt.G - column name is `Id`, and in model class it's `Id`; momo - I add the error massege and i make like you say and have in view the same problem –  Mar 05 '19 at 13:58
  • @Andrei can you delete the entire database and let .NET Core recreate it for you? (EF Code-First approach) – momo Mar 05 '19 at 14:38

1 Answers1

0

How are you doing your entity framework configuration? It's been a while since I worked with it but I seem to remember you have to explicitly name your foreign keys.

Take a look at this answer

https://stackoverflow.com/a/28001707/385965

PhilH
  • 60
  • 2
  • 7