简体   繁体   中英

EF 6 and Web API Error adding entity with an existing entity

I create a REST service using the Web API and I'm using EF Code First. My problem is when I add an entity with an existing relationship foreignId = 1 , EF add to me the relationship and returns my entity with foreignId = 2 .

POST object
{
 "nom":"Test",
 "actif":true,
 "provinceId":1,
 "province":{
    "id": 1,
    "nom": "Province ok",
    "actif": true
 }
}

Return object

    {
        "id": 1,
        "nom": "Test",
        "actif": true,
        "provinceId": 2,
        "province": {
            "id": 2,
            "nom": "Province ok"
        },

    }

I would also used a repository pattern of this style.

     public abstract class ServiceCrud<TEntity> : ServiceBase
            where TEntity : EntityBase
        {

            public virtual object Get()
            {
                // Context.
                return Ctx.Set<TEntity>().OrderBy(x => x.Nom).ToList();

            }

            public virtual async Task<object> Get(int id)
            {
                // Context.
                var entity = await Ctx.Set<TEntity>().FindAsync(id);

                // Return.
                return entity;
            }

            public virtual async Task<object> Add(TEntity entity)
            {
                // Check Validity.
                CheckValidity(entity);

                // Context.
                Ctx.Set<TEntity>().Add(entity);
                await Ctx.SaveChangesAsync();

                // Return.
                return entity;
            }

            public virtual async Task<object> Update(TEntity entity)
            {
                // Check Validity.
                CheckValidity(entity);

                // Context.
                Ctx.Entry(entity).State = EntityState.Modified;
                await Ctx.SaveChangesAsync();

                // Return.
                return entity;
            }

            public virtual void CheckValidity(TEntity entity)
            {
                // Nom unique.
                var entityBase = Ctx.Set<TEntity>().AsNoTracking().FirstOrDefault(x => x.Nom == entity.Nom);

                if (entityBase != null && (entity.Id == null || entity.Id != entityBase.Id))
                    throw new ValidationException("Le nom doit être unique");

            }
        }

Thank you for your help.

Alex

If I override my method, that is ok.

public override async Task<object> Add(Commune entity)
    {
        // Check Validity.
        CheckValidity(entity);

        // Context.
        Ctx.Set<Commune>().Add(entity);
        Ctx.Set<Province>().Attach(entity.Province);

        await Ctx.SaveChangesAsync();

        // Return.
        return entity;
    }

But is there not a more generic method?

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