Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding class to EF Model cause compile error "The type or namespace name <className> could not be found"

Strange one, I've already added 5 tables with no problem. However, when I try and add my PostCodesUK table through my edmx model, it gives me this error message:

Error   1   The type or namespace name 'PostCodesUK' could not be found (are you missing a using directive or an assembly reference?)   d:\Ben\documents\visual studio 2013\Projects\DogWalks\DogWalks\App_Code\WalksModel.Context.cs   33  30  DogWalks

Not sure what is happening but there are some subtle differences. For example, if I look under the WalksModel.tt classes, I can see the PostCodesUK class, but it hasn't generated the additional 'stub':

enter image description here

If I then double click the error above, it takes me to the WalksModel.Context.cs where I see this error: enter image description here

I've looked at the namespace under the PostCodesUK.cs class, which matches the other classes. Does anyone know what the issue is? I have searched online for the issue but could not find a solution.

Update: This is what the PostCodeUK class looks like:

namespace DogWalks.App_Code
{
    using System;
    using System.Collections.Generic;

    public partial class PostCodesUK
    {
        public int PostcodeID { get; set; }
        public string Postcode { get; set; }
        public decimal Latitude { get; set; }
        public decimal Longitude { get; set; }
        public string PostcodeNoSpace { get; set; }
    }
}

Furthermore, I've added another temporary table (Person) to my model to test it and it's also giving me the same error message as PostCodesUK:

public virtual DbSet<Comment> Comments { get; set; }
...
public virtual DbSet<PostCodesUK> PostCodesUKs { get; set; } //error
public virtual DbSet<Person> People { get; set; } //error
like image 944
benscabbia Avatar asked Dec 10 '25 23:12

benscabbia


1 Answers

Ok after spending countless hours trying to find a solution I may have actually solved it (touch-wood).

I got frustrated so I deleted my edmx model. Reading online, I found there was a bug with VS 2012 when the edmx model was saved in a subfolder (not in the root). So this might be something to try if my solution below doesn't work, although I managed to keep it in a subfolder.

Anyway, after deleting my model:

  1. I readded the whole database model to a new folder called DAL. When I tried to compile, I noticed ALL of my classes were now throwing the same error.
  2. I changed all of the build action for all the classes (From Content to Compile)
  3. Rebuilt app and it worked.

So to put it to the test I tried:

  1. Adding another table to my model, Person.
  2. Tried to compile (it actually compiled ok) but when I looked at the Person.cs file under Model.tt I saw the stub was once again missing:

enter image description here

  1. I changed the Build Action setting to Compile, saved, closed VS and reopened it and the stub appeared correctly: enter image description here

I don't know if that was the issue, but it seems to be working. I didn't check my original case but I have a suspicion that I changed the .cs compile mode for my original classes, but not for the newly added classes.

I'm just glad I held off from turning my machine into scrap metal.

like image 168
benscabbia Avatar answered Dec 13 '25 12:12

benscabbia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!