0

enter image description here

My code only lists what the database has at the moment, but I need to insert data into it.

namespace WebApplication5.Controllers
{
    public class DefaultController : ApiController
    {
        public IEnumerable<Table_1> Get() 
        {
            using (testEntities entities = new testEntities())
            {
                Table_1 us = new Table_1() { id = "221", name = "asdsadasdsad" };
                entities.Table_1.Add(us);
                entities.SaveChangesAsync();

                return entities.Table_1.ToList();
            }
        }
    }
}

normally db updates with that query

Insert into Table_1 values ('111','222')

id (string)
name(string)
Matthijs
  • 2,483
  • 5
  • 22
  • 33
xifoleyib
  • 1
  • 3
  • it gives valdation exception – xifoleyib Dec 16 '18 at 10:13
  • 2
    Please provide the **complete and exact** exception (including all `.InnerException` and `.Errors` details) – marc_s Dec 16 '18 at 10:24
  • + e {"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."} System.Data.Entity.Validation.DbEntityValidationException – xifoleyib Dec 16 '18 at 10:28
  • + e.EntityValidationErrors Count = 1 System.Collections.Generic.IEnumerable {System.Collections.Generic.List} – xifoleyib Dec 16 '18 at 10:28
  • And what do these `EntityValidationErrors` tell you?? – marc_s Dec 16 '18 at 10:29
  • + entities {ClassLibrary1.testEntities} ClassLibrary1.testEntities + entities.Table_1 "System.Data.Entity.DbSet`1[ClassLibrary1.Table_1]" System.Data.Entity.DbSet + this {WebApplication5.Controllers.DefaultController} WebApplication5.Controllers.DefaultController + us {ClassLibrary1.Table_1} ClassLibrary1.Table_1 – xifoleyib Dec 16 '18 at 10:34
  • i have updated the questinon with error screenshot – xifoleyib Dec 16 '18 at 10:35
  • We still haven't seen what is actually mentioned in the `EntityValidationErrors` - that's where EF would **tell you** in clear text English what the problem is ...... – marc_s Dec 16 '18 at 10:36
  • how can i find that where – xifoleyib Dec 16 '18 at 10:37
  • i think the string length is more then 10 letters it givers an error. but its a nvarchar max? – xifoleyib Dec 16 '18 at 10:46
  • System.Data.Entity.Infrastructure.DbUpdateException: 'Unable to update the EntitySet 'Table_1' because it has a DefiningQuery and no element exists in the element to support the current operation.' – xifoleyib Dec 16 '18 at 10:48
  • See [this other SO question](https://stackoverflow.com/questions/7583770/unable-to-update-the-entityset-because-it-has-a-definingquery-and-no-updatefu) about exactly this error message - any chance one of these three things in the accepted answer applies here? Is `Table_1` a **view**, by any chance? Or doesn't have a **primary key**?? – marc_s Dec 16 '18 at 11:07
  • 1
    ok fixed created PK. and regenerated the model now worls i can insert into db what about select,update and delete – xifoleyib Dec 16 '18 at 11:19
  • Tables in EF (and in general) always should have a primary key - and once you have that, EF can do everything with the data in the table - all operations should work just fine, once your table has a PK – marc_s Dec 16 '18 at 11:42
  • yes, now how can i pass a paramter into name [HttpGet] public IEnumerable Get() { using (testEntities2 entities = new testEntities2()) { Table_1 us = new Table_1() { id=0, name="{name}"}; entities.Table_1.Add(us); entities.SaveChanges(); return entities.Table_1.ToList(); } – xifoleyib Dec 16 '18 at 12:27
  • This is a totally different thing --> put that into a new question! – marc_s Dec 16 '18 at 12:28

1 Answers1

0

Please use a try-catch block and find the validation error that cause above error.

try
{
    using (testEntities entities = new testEntities())
    {
        using (testEntities entities = new testEntities())
        {
            Table_1 us = new Table_1() { id = "221", name = "asdsadasdsad" };
            entities.Table_1.Add(us);
            entities.SaveChangesAsync();

            return entities.Table_1.ToList();
        }
    }

}
catch (DbEntityValidationException ex)
{
    foreach (var ve in ex.EntityValidationErrors)
    {
        foreach (var err in ve.ValidationErrors)
        {
            Console.WriteLine("Property Name: \"{0}\", Error Message: \"{1}\"", err.PropertyName, err.ErrorMessage);
        }
    }
    throw;
}
Dumi
  • 1,414
  • 4
  • 21
  • 41