简体   繁体   中英

The specified type member 'UserName' is not supported in LINQ to Entities

I am trying to show images from a specific user by using the id parameter in the url but I get this error message:

The specified type member 'UserName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

This is my action method:

[HttpGet]
public ActionResult Show(String id)
{
    var images = new List<Image>();
    var db = new portfolio_project_db();

    if (String.IsNullOrEmpty(id))
    {
        return RedirectToAction("Index", "Home");
    }
    else
    {
        images = db.Images.Where(x => x.UserName == id).ToList();

        return View(images);
    }
}

I have read more about this error and from what I understand people get this because the variable is not a column in the database, but I do have an UserName field in my table Image.

public partial class Image
{
    public int ImageID { get; set; }
    public string Title { get; set; }
    public string ImagePath { get; set; }
    public Nullable<int> UserID { get; set; }
    public string UserName { get; set; }

}

CREATE TABLE [dbo].[Image] (
    [ImageID]   INT            IDENTITY (1, 1) NOT NULL,
    [Title]     VARCHAR (50)   NULL,
    [ImagePath] NVARCHAR (MAX) NULL,
    [UserID]    INT            NULL,
    [UserName] NVARCHAR(20) NOT NULL, 
    PRIMARY KEY CLUSTERED ([ImageID] ASC)
);

What do I need to do differently?


Requested:

    public partial class portfolio_project_db : DbContext
    {
    public portfolio_project_db()
        : base("name=portfolio_project_db")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Image> Images { get; set; }
}

The specified type member 'UserName' is not supported in LINQ to Entities message indicates that the property UserName is still not mapped to UserName column in database. Here are the steps to enable property mapping in Database First configuration (because UnintentionalCodeFirstException() exists in OnModelCreating() method):

a. Using EDMX designer (recommended)

1) Remove existing UserName property from Image data model class.

2) Open the EDMX file of your model in entity designer, then right click in any empty space of designer surface and choose Update Model from Database option.

3) Save all changes in EDMX designer, now the UserName property is automatically created and mapped with UserName column in database.

b. Edit EDMX file to create mapping (advanced - not recommended)

This approach requires editing the EDMX file to add mapping property manually by adding property name in both <edmx:StorageModels> , <edmx:ConceptualModels> and <edmx:Mappings> section:

<!-- for edmx:StorageModels -->
<Property Name="UserName" Type="nvarchar" MaxLength="20" Nullable="false" />

<!-- for edmx:ConceptualModels -->
<Property Name="UserName" Type="String" MaxLength="20" FixedLength="false" Unicode="true" Nullable="false" />

<!-- for edmx:Mappings -->
<ScalarProperty Name="UserName" ColumnName="UserName" />

Note: Every time you added or updated column(s) and/or table schema in database, you should update the model to reflect all changes from database into data model classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM