简体   繁体   English

具有单个ID实体框架的.Find上的主键错误

[英]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. 我在使用单个PK的实体上执行.find时遇到问题。

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; 但是,当我尝试在制造商上进行HTTPPUT时,出现以下错误;

The number of primary key values passed must match number of primary key values defined on the entity. 传递的主键值的数量必须与在实体上定义的主键值的数量匹配。 Parameter name: keyValues 参数名称:keyValues

This is thrown when I call the .find method on the set; 当我在集合上调用.find方法时,会抛出该异常;

    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) 数据库已生成(这是通过VS表设计完成的)PK_dbo.Manufacturers(主键,群集:Id)

I am operating a SOLID architecture, where my controller is as follows; 我正在使用SOLID架构,我的控制器如下:

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

        return response.Result;
    }

where the ManufacturerService is as follows; 其中ManufacturerService如下;

    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. 其中obj是传递的要更新的制造商。 and the id is an integer. 并且id是整数。

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). 如果在制造商上失败,并且唯一的列是名称,那么将您的调用堆栈归咎于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. 请尝试将Entity类中的代码复制到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. 您将两个键值传递给“查找”,但是EF仅能推断出Id是PK。 Do you really want a nullable bool as part of your PK? 您是否真的想将可为空的布尔值作为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. 如果您确实希望将可为空的布尔作为密钥的一部分,那么我认为您需要让EF知道这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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