views:

151

answers:

3

Hello,

I've created a one table contact DB, which has only 3 columns (Id, Name, and Phone). I've then created the ContactsDataContext using my table Contacts in the model folder. Finally, I create a partial class still in the model folder (public partial class Contact).

now when I write this

public partial class Contact
{
    public string MyContact
    {
        get 
        {
            string name = this.Name ?? String.Empty; 
        }
        // ... Other lines omitted
    }
}

I get the following error :"'ContactsManager.Models.Contact' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'ContactsManager.Models.Contact' could be found (are you missing a using directive or an assembly reference?)"

Is something wrong??? Even the Intellisense in not showing the properties from my DataContext class. Yet, I've written some partial classes in the past with no problem.

Thank you.

+1  A: 

Are namespaces the same on the two partials?

H4mm3rHEad
I just checked both classes have the same namespace (namespace ContactsManager.Models)
Richard77
A: 

Did you get this resolved? I'm having the same issue.

Chris Roden
A: 

Chris Roden,

Yes, I've resolve it. In fact, I've asked the above question many months ago when I started learning ASP.NET MVC. I bought a book called "ASP.NET MVC - The Beer House/Nick Berardi/Wrox." Is a god book, but it's not recommendable for beginners. Generaly, things are thrown like that without telling where they come from.

The response came from applying the definition of a partial class. Among others, partial classes must:

  • have the same name
  • be preceded by 'partial' keyword,
  • be defined in the same namespace,
  • etc.

If you miss any of the above criteria, then you'll be in trouble because those criteria, ll be used to merge all the partial classes into a unique one.

In my case, I created a table called ContactDB. After I've created the datacontext class, I've dropped the ContactDB table on the Linq2Sql editor. As you must know, that creates the following class:

public partial class ContactDB
{
  //All the columns in the table become properties in this class
} 

The partial keyword allow me to write this:

public partial class ContactDB
{
  //I can reference members of the above partial class... using this keyword
  //After all, the 2 constitute one class.
} 

After reading the definition of partial classes, I found out that I failed one of the criteria. I called my other partial class "Contact" which's different from ContactDB. That was enough to make me go crazy for 3 days until I read the definition. Moreover, if you defines the partial class with the right name but you put it in a different namespace, you'll get in trouble as well.

So, if the above answer doesn't work for you (I don't know exactly your problem), check the definition of the partial class.Don't forget to read the ScottGu's series on Linq2Sql.

Hope it helps.

Richard77