.Net – Entity Framework – error CS0246: The type or namespace name ‘DatabaseGenerated’ could not be found

Background

Here is our first post on adding annotations to our plain old C# class ( POCO).

Lineage

  1. .Net – Entity Framework – error CS0246: The type or namespace name ‘DatabaseGenerated’ could not be found
    Link

Error

Error Text


error CS0246: The type or namespace name 'DatabaseGenerated' could not be found (are you missing a using directive or an assembly reference?)

error CS0246: The type or namespace name 'DatabaseGeneratedAttribute' could not be found (are you missing a using directive or an assembly reference?)

error CS0246: The type or namespace name 'Column' could not be found (are you missing a using directive or an assembly reference?)

error CS0246: The type or namespace name 'ColumnAttribute' could not be found (are you missing a using directive or an assembly reference?)

error CS0246: The type or namespace name 'Table' could not be found (are you missing a using directive or an assembly reference?)

error CS0246: The type or namespace name 'TableAttribute' could not be found (are you missing a using directive or an assembly reference?)

Error Image

CS0246.01.20191119.0208AM

TroubleShooting

Source Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data.Entity;
using System.Data.Entity.Core;

[Table("friend", Schema="dbo")]
public class friend
{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }

    [Key]
    [Column(Order=1)]
    public string name { get; set; }

    public string phoneCell { get; set; }

    public string address1     { get; set; }
    public string address2     { get; set; }
    public string city         { get; set; }
    public string state        { get; set; }
    public string postalCode   { get; set; }                

}

Source Code – Included Namespaces

Here are the originally included namespaces :-

Image

CS0246.namespace.01.20191119.0222AM

Textual

  1. System
  2. System.Collections.Generic
  3. System.Linq
  4. System.Web
  5. System.Data.Entity
  6. System.Data.Entity.Core

 

Source Code – Namespaces Needed

Outline

We added a couple of Data Annotations specific namespaces :-

  1. System.ComponentModel.DataAnnotations
  2. System.ComponentModel.DataAnnotations.Schema

 

Image

CS0246.namespace.02.20191119.0227AM

Textual

  1. System
  2. System.Collections.Generic
  3. System.Linq
  4. System.Web
  5. System.Data.Entity
  6. System.Data.Entity.Core
  7. System.ComponentModel.DataAnnotations;
  8. System.ComponentModel.DataAnnotations.Schema;

 

Summary

So for Day 01, please be sure to reference the System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema namespaces in your source code.

One thought on “.Net – Entity Framework – error CS0246: The type or namespace name ‘DatabaseGenerated’ could not be found

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s