Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:38:56+00:00 2026-06-07T15:38:56+00:00

Trying to get my head around Entity Framework. I have a database with two

  • 0

Trying to get my head around Entity Framework. I have a database with two tables containing information on transactions. One contains transaction category information and is linked 1-to-1 into the transaction table (transactions.Category <=> Category.CategoryID). I can create new transactions in memory with the category entity filled but when I try to flush to DB I get “{“Invalid object name ‘mEconomyUser.category’.”}”

Transactions table:

TransactionID   uniqueidentifier
UserID      uniqueidentifier
Date        date
Text        nvarchar(250)
Category    uniqueidentifier
Amount      decimal(18, 0)

Category Table:

CategoryID  uniqueidentifier
UserID      uniqueidentifier
Text        nvarchar(50)

Here are my models:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace mEconomy.Models
{
    [Table("transactions", Schema = "mEconomyUser")]
    public class Transaction
    {
        [Key]
        public Guid TransactionID { get; set; }
        public Guid UserID { get; set; }
        public DateTime Date { get; set; }
        public string Text { get; set; }
        public virtual Category Category { get; set; }
        public decimal Amount { get; set; }
    }


    public class TransactionDBContext : DbContext
    {

        public DbSet<Transaction> Transactions { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace mEconomy.Models
{
    [Table("category", Schema = "mEconomyUser")]
    public class Category
    {
        [Key]
        [ForeignKey("Transaction")]
        public Guid CategoryID { get; set; }
        public Guid UserID { get; set; }
        public String Text { get; set; }

        public virtual Transaction Transaction { get; set; }
    }

    public class CategoryDBContext : DbContext
    {

        public DbSet<Transaction> Categorys { get; set; }
    }
}

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:38:58+00:00Added an answer on June 7, 2026 at 3:38 pm

    Thank you everybody for pushing me in the right direction! I now sorted all the issues with my code. Almost a little embarrassing but this was my very first encounter with EF. So this is what I ended up with:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations;
    
    namespace mEconomy.Models
    {
        [Table("Transactions", Schema = "mEconomyUser")]
        public class Transaction
        {
            [Key] // Define primary-key
            public Guid TransactionID { get; set; }
            public Guid UserID { get; set; }
            public DateTime Date { get; set; }
            public string Text { get; set; }
            public decimal Amount { get; set; }
    
            [Column("Category")] // Column name of the database foreign key is named Category, want to use that name for the Model
            public Guid? CategoryID { get; set; } // Property to store the foreign key for the Category
            [ForeignKey("CategoryID")] // Define property that holds the foreign key for the category model/object
            public virtual Category Category { get; set; }
        }
    
        [Table("Categorys", Schema = "mEconomyUser")]
        public class Category
        {
            [Key] // Define primay-key
            public Guid CategoryID { get; set; }
            public Guid UserID { get; set; }
            public String Text { get; set; }
    
            public virtual ICollection<Transaction> Transactions { get; set; }
        }
    
        public class TransactionDBContext : DbContext
        {
    
            public DbSet<Transaction> Transactions { get; set; }
            public DbSet<Category> Categorys { get; set; }
        }
    }
    

    Okej a quick rundown of the problems I had.

    As GertArnold pointed out, copy & past error! I had two DbSets of type Transaction instead of one each as above.

    Two make sure that the code understands the one-to-many, or one-category-to-many-transactions, connection I had to add a ICollection of Transactions on the Category model.

    On the transaction option there where nowhere to store the foreign key for the categoy model. I added a Guid (CategoryID) to store it in.

    The EF tried to find a Column named Category_CategoryID in the Transactions table to store the foreign key for the corresponding Category entry. Since I used a database already in existence I had to use data annotations to set the Column for the CategoryID field.

    I had to do the CategoryID Guid? or nullable. Otherwise all transactions that didn’t have a Category would try to add a category into the database with GUID = 000-000…. Witch gave me a primary key constraint error.

    And yes I know that Categories are miss spelled… done the same mistake since high-school.

    The information you guys provided me with gave me a better understanding of EF so I was able to Google the right words. Found an blog series from a user here that really helped me get into it! – Thanks Morteza Manavi for the great input!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to get my head around the entity framework 4.1 codefirst approach for
I'm trying to get my head around WCF transactions but struggling with one concept.
trying to get my head around Feedzirra here. I have it all setup and
I'm trying to get my head around the covariance of Scala's collections. I have
I'm trying to get my head around the addressing of WCF services. We have
am trying to get my head around the following: Have a small program am
Im trying to get my head around MySQL. I have learnt a great deal
I'm trying to get my head around solving the following problem. I have the
I am trying to get my head around the Redis/NoSQL way or designing database
Im trying to get my head around CDI and EJB and the Entity Boundary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.