Home

Able to query but not insert

I'm trying to insert a record using entity framework context. I was able to find one mention of that here: https://forum.linqpad.net/discussion/129/inserting-records

I can query AgentEmail table as such:
var agentEmails = AgentEmail.Take(10);
agentEmails.Dump("Ten records");

Inserts fail:
AgentEmail newEmail = new AgentEmail { AgentId = 462, EmailTypeId = 3, EmailAddress = "my.email@co.uk" };

AgentEmail.InsertOnSubmit(newEmail);
SubmitChanges();

What I get is CS0246 The type or namespace name 'AgentEmail' could not be found (press F4 to add a using directive or assembly reference)

I'm not understanding why I can query but not insert.

Comments

  • If you mouse-over the table name in the connection pane, you can see the actual name of the generated class. Is it Table<AgentEmail>? Or something else?

    image
  • It is just AgentEmail in the popup. Does not have Table<>
  • Ah, Table<> is for LINQ-to-SQL generated data context.

    For EF the code is a bit different. Doesn't explain why you get the 'Type could not be found' error. If it's visible in the tree view, I'd assume the type is in the current namespace.

    Or, did you define another namespace and failed to register it under F4 (Query Properties), Additional Namespace Imports?
    var newEmail = new AgentEmail { AgentId = 462, EmailTypeId = 3, EmailAddress = "my.email@co.uk" };
    AgentEmail.Add(newEmail);
    SaveChanges();
  • I had added references to my .dll in the additional references pane instead of adding namespace imports. I changed that and now I get:

    CS1061 'DbSet' does not contain a definition for 'InsertOnSubmit' and no extension method 'InsertOnSubmit' accepting a first argument of type 'DbSet' could be found (press F4 to add a using directive or assembly reference)

    looks a little like progress.
  • More specifically, I have namespace imports for my domain project which contains data models and my data project which contains my context and configurations.
  • edited November 2018
    Have you looked at my (EF 6) example?

    You linked a LINQ-to-SQL example, but are using Entity Framework. They use different methods. My example uses EF 6 methods (Table.Add and SaveChanges). EF 5 would require AddObject and SaveChanges).
  • That actually works. Thank you.

    This is a great tool with little to no documentation. I would actually pay for this if I would get some documentation for the fee.
  • Paid version has intellisense, which is much more helpful than documentation (imo) :)
Sign In or Register to comment.