简体   繁体   中英

Primary Key Error on .Find with single Id Entity Framework

I am having an issue with performing a .find on an entity with a single PK.

The base class is as follows;

public abstract class Entity : IEntity
{
    public int Id { get; set; }

    public bool? IsArchived { get; set; }
}

and the class is

public class Manufacturer : Entity
{
    public string Name { get; set; }
}

however, when i attempt a HTTPPUT on the Manufacturer i get the following error;

The number of primary key values passed must match number of primary key values defined on the entity. Parameter name: keyValues

This is thrown when I call the .find method on the set;

    public T Find<T>(int id, params Expression<Func<T, object>>[] includes) where T : class, IEntity
    {
        return this.Set<T>().Find(id, includes);
    }

Any ideas why this is occurring?

Update

I am running Code first. The database has generated (this is through VS table design) PK_dbo.Manufacturers (Primary Key, Clustered: Id)

I am operating a SOLID architecture, where my controller is as follows;

    [HttpPut]
    [Route("")]
    public Manufacturer Put(Manufacturer m)
    {
        var response = ManufactureService.UpdateManufacturer(m);

        return response.Result;
    }

where the ManufacturerService is as follows;

    public ServiceResponse<Manufacturer> UpdateManufacturer(Manufacturer obj)
    {
        Func<Manufacturer> func = delegate
        {
            using (var context = _contextFactory())
            {
                var manufacturer = context.Find<Manufacturer>(obj.Id);
                manufacturer.Name = obj.Name;
                manufacturer.IsArchived = manufacturer.IsArchived;

                context.Update(manufacturer);

                return manufacturer;
            }
        };

        return this.Execute(func);
    }

where obj is the passed Manufacturer to be updated. and the id is an integer.

to give a little more info, I have a contect which then uses the following;

    public T Find<T>(int id, params Expression<Func<T, object>>[] includes) where T : class, IEntity
    {
        return this.Set<T>().Find(id, includes);
    }

If its failing on Manufacturer, and the only column is Name, then it makes zero sense that your call stack is blaming Find(int id). There is no integer primary key on this table. Maybe that is your problem.

UPDATE

Please try copying the code in the Entity class into Manufacturer. Then remove the subclass relationship. Then see if your put still fails.

You pass two key values to Find but EF would only deduce that Id is a PK. Do you really want a nullable bool as part of your PK? I'm not sure if this is allowed. If you do want the nullable bool as part of your key then I think you will need to let EF know about it.

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