Results 1 to 4 of 4

Thread: save model from another model

  1. #1

    Thread Starter
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    save model from another model

    how can i save my model to another same model ?

    Code:
    [HttpPost]
    public ActionResult Create(Employee employee)
    {
          if (ModelState.IsValid)
         {
                db.Employees.Add(employee);
                db.Employees_log.Add(employee); ===> error: "unable to update entityset because it has a definingquery and no insertfunction"
                db.SaveChanges();
         }
    }
    I basically wanted to save two models at the same time.
    Last edited by VBKNIGHT; Apr 25th, 2013 at 01:08 AM.

    If a post has helped you then Please Rate it!

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: save model from another model

    When a table doesn't have a primary key it is treated as a view. Since it's treated as a view it'll show up in the defining query section of the edmx. Try following the example here to fix it: http://blogs.msdn.com/b/alexj/archiv...ble-views.aspx
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3

    Thread Starter
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Re: save model from another model

    well after spending so much time with it i finally get it to save the records yes it's true you need to have an key in your table and remove the defining query.

    my question is why is my edmx file the (model class specifically) change to default? even if i edited the file and put attributes on each field using visual studio 2012.

    is there a correct tool or way of editing the edmx file? should it only be updated once then manually change it if there's an need to?

    If a post has helped you then Please Rate it!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: save model from another model

    Quote Originally Posted by VBKNIGHT View Post
    well after spending so much time with it i finally get it to save the records yes it's true you need to have an key in your table and remove the defining query.

    my question is why is my edmx file the (model class specifically) change to default? even if i edited the file and put attributes on each field using visual studio 2012.

    is there a correct tool or way of editing the edmx file? should it only be updated once then manually change it if there's an need to?
    If you make changes to the database then you open the model in the designer, right-click the empty space and select Update From Data Source or the like. That will re-run the original wizard and your updated schema will be imported. That said, some things will not change, e.g. if you have renamed a column then the new name will be added but the old name will remain. Persoanlly, I often just delete entities for tables that have changed to ensure that there's no rubbish left behind. In fact, you can simply delete the whole model and regenerate it from scratch. If the names are the same then all your existing will just work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width