Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding entity frame model to a project

I have problem adding entity framework model to my project. Here is what I am doing:

1- Right click on project

2- Select add

3- In dialog select data from installed templates.

4- in installed template I cannot see ADO.NET entity framework template.

What should I install?

I use NuGet to install Entity framework 4.2.0.0 but no success.

I am using Visual Studio 2010

EDIT: Look for in the comment of answer for information.

like image 405
mans Avatar asked Oct 09 '22 22:10

mans


1 Answers

Which method of Entity Framework are you trying to use? The most straightforward (in my opinion) is CodeFirst.

DataBaseFirst or ModelFirst

If you are using the wizard to create a model,

  1. Right-click the project > Add New Item
  2. In whichever language you are using, there should be a Data node. Under that node, select ADO.NET Entity Data Model.
  3. Use the designer or wizard to model your ORM mapping

CodeFirst

(you can do this with an existing database, so the name is a bit of a misnomer)

  1. Right-click the project > Add Class
  2. Name it for one of your planned business objects (if using an existing database, classes can be mapped by name if they match tables in the database exactly)
  3. Outline properties (if using an existing database, properties can be mapped by name if they match fields in the database exactly)
  4. Right-Click on the project > Add Library Package Reference
  5. Under Online>All search for Entity, and install the Entity Framework package (if already installed, it may just need to be referenced.
  6. You may need to resolve using statements (or include if using VB.NET) in your entity class(es).

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

namespace Kiersted.Keps.BusinessObjects
{
    [Table("Search", Schema = "mySchema")]
    public class BatchSearch : KepsBusinessObject, IKepsBusinessObject
    {

        public BatchSearch() { }

        public BatchSearch(DateTime created)
        {
            this.Created = created;
        }

        #region Data Properties

        [Key]
        [Column("BatchSearchID")]
        public int SearchId{ get; set; }

        [Column("uidQueueMaster")]
        public Nullable<int> uidQueueMaster { get; set; }

        [Column("DateCreated")]
        public DateTime Created { get; set; }

        [Column("DateCompleted")]
        public Nullable<DateTime> Completed{ get; set; }

        public string QueryTerms { get; set; }

        [NotMapped]
        public string LocalProperty { get; set; }
    }
}

Note: If you are using an existing database, you can either name your classes the same as your tables or add the Table attribute to the class declaration. If you are putting your tables into a different schema (default is dbo) then you will need the Table tag regardless of the name so that you can specify the schema.

Note: If you are using and existing database, you can either name your properties the same as the corresponding fields or you can add the Column attribute.

like image 103
CodeWarrior Avatar answered Oct 12 '22 09:10

CodeWarrior