1

In .NET 4.0 framework the System.ComponentModel.DataAnnotations.Schema doesn't work or doesn't support I think.

I am using a code-first approach and my database already exists, and also not using ADO.net Entity Data Model.

I already used the [Table] attribute and DatabaseGenerated attribute, it's not working causing a compilation error to occur.

This is my code:

Entity class:

public class myclass
{
    public myclass()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    [Key]
    public int RECORDID { get; set; }
    public string AA { get; set; }
    public string CAT { get; set; }
    public string CS { get; set; }
    public int? FS { get; set; }
    public int? CA { get; set; }
    public int? DR { get; set; }
    public int? UM { get; set; }
    public int? ID { get; set; }
    public double LAT { get; set; }
    public double LON { get; set; }
    public int? NIC { get; set; }
}

DbContext class:

public class classContext : DbContext
{
    public classContext() : base("name=DBConnection")
    {
        //Disable initializer
        Database.SetInitializer<classContext>(null);
    }

    public DbSet<myclass> myclasses { get; set; }
}

Or I have to add this override method of model creating, is it helpful or not?

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<myclass>().ToTable("Datatbl");
}

Help me out. Thanks

1

1 Answer 1

1

You can Use Table attribute over your class:

[Table("Datatbl")]
public class myclass
{
}
Sign up to request clarification or add additional context in comments.

3 Comments

#manoj : the table Attribute use System.ComponentModel.DataAnnotations.Schema namespace or i am using the 4.0
Yes it uses the System.ComponentModel.DataAnnotations.Schema
but its not there in the Reference library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.