milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. ASP.NET
  4. MVC 3

DbContext

Details
Written by: Stanko Milosev
Category: MVC 3
Published: 05 August 2012
Last Updated: 08 January 2013
Hits: 3784

If your model look something like:

using System; 
using System.Data.Entity; 
using System.ComponentModel.DataAnnotations; 
using System.Data.Entity.Infrastructure;

namespace MvcApplication3.Models
{
    public class MyTable
    {
        [Key]
        public int MyNum { get; set; }
        public string Name { get; set; }
    }

    public class MyTableDBContext : DbContext
    {
        public DbSet MyTable { get; set; }
    }
}

Then, your connection string in Web.config must look like:

 <add name="MyTableDBContext" connectionString="Data Source=MyPC;
													Initial Catalog=MyDb;
													Integrated Security=False;
													Persist Security Info=True;
													User ID=myUser;
													Password=myPass" 
		 providerName="System.Data.SqlClient" />

Note that MyTableDBContext is in connection string and defined as DbContext in the model, and Integrated Security is false (true means to Windows Authentication)

Non-identity id key

Details
Written by: Stanko Milosev
Category: MVC 3
Published: 31 May 2012
Last Updated: 05 August 2012
Hits: 5541

Inserting in non identity ID column:

Table:

CREATE TABLE [dbo].[TestTable](

[Id] [int] NOT NULL,

[Name] [nvarchar](50) NULL,

CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED

(

[Id] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

Model:

using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace NoIdent.Models

{

	[Table("TestTable")]
	public class TestTable

	{

		[DatabaseGenerated(DatabaseGeneratedOption.None)]
		public int Id { get; set; }
		public string Name { get; set; }

	}

	public class testNoIdentEntities : DbContext

	{	

		public DbSet TestTable { get; set; }

	}

}

Without [Table("TestTable")], EF will create TestTableS - EF always add S (plural)

[DatabaseGenerated(DatabaseGeneratedOption.None)] is mandatory, to tell EF that ID is not identity

Create controller with scaffolding. Modify Create and Index view:

Create:

	<fieldset>

		<legend>TestTable</legend>

		<div class="editor-label">
			@Html.LabelFor(model => model.Id)
		</div>

		<div class="editor-field">
			@Html.EditorFor(model => model.Id)
			@Html.ValidationMessageFor(model => model.Id)
		</div>

		<div class="editor-label">
			@Html.LabelFor(model => model.Name)
		</div>

		<div class="editor-field">
			@Html.EditorFor(model => model.Name)
			@Html.ValidationMessageFor(model => model.Name)
		</div>

		<p>
			<input type="submit" value="Create" />
		</p>
	</fieldset>

Index:

<table>
	<tr>
		<th>
			Id
		</th>

		<th>
			Name
		</th>

		<th></th>

	</tr>

	@foreach (var item in Model) {
		<tr>
			<td>
				@Html.DisplayFor(modelItem => item.Id)
			</td>
	
			<td>
				@Html.DisplayFor(modelItem => item.Name)
			</td>	
			
			<td>
				@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
				@Html.ActionLink("Details", "Details", new { id=item.Id }) |
				@Html.ActionLink("Delete", "Delete", new { id=item.Id })
			</td>
		</tr>
	}

</table>

and that's all folks!

Example

Details
Written by: Stanko Milosev
Category: MVC 3
Published: 31 May 2012
Last Updated: 31 May 2012
Hits: 3662
If your table look like:
CREATE TABLE [dbo].[_Partner](
	[Sifra] [int] NOT NULL,
	[Naziv] [nvarchar](70) NULL,
	[Mesto] [nvarchar](50) NULL,
	[Pib] [nvarchar](20) NULL,
	[Adresa] [nvarchar](50) NULL,
	[Region1] [int] NULL,
	[Region2] [int] NULL,
	[Region3] [int] NULL,
	[HRabat] [decimal](18, 2) NULL,
 CONSTRAINT [PK__Partner] PRIMARY KEY CLUSTERED 
(
	[Sifra] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
Then your model should look like:
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace MvcMovie.Models
{
    [Table("_Partner")]
    public class Partner
    {
      [Key]
      public int Sifra { get; set; }
      public string Naziv { get; set; }
      public string Mesto { get; set; }
      public string Pib { get; set; }
      public string Adresa { get; set; }
      public int? Region1 { get; set; }
      public int? Region2 { get; set; }
      public int? Region3 { get; set; }
      public decimal? HRabat { get; set; }
    }

    public class PartnerDBContext : DbContext
    {
        public DbSet Partners { get; set; }
    }
}

Where int? and decimal? are fields with NULL values.

One example of connection string in web.config is like:

    

Special characters in model

Details
Written by: Stanko Milosev
Category: MVC 3
Published: 23 May 2012
Last Updated: 30 November -0001
Hits: 3867
If you have for name of table something like _Partner, like me :), then use [Table("_Partner")], for Key [Key]... Using System.ComponentModel.DataAnnotations From here.
  1. Adding controller
  2. Publish application on windows azure (free)
  3. DateTime localization
  4. Special characters

Subcategories

Razor

Page 4 of 6

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6