Kinda asking everything in a subject of this post. I'm wondering if it is possible to include Microsoft.Data.Entity.CTP.dll reference to WebMatrix site and if the Compact edition of the database would support it? I'd kinda love use something like
code-first development in my WebMatrix project. I think is even easier than writing SQL-s into the code...
Also I'm reading that Linq isn't supported yet and I'd really like to know if it's planed to include it?
First you need to add Microsoft.Data.Entity.CTP.dll in ~/bin. Then create a database if you don´t already have one.After that you add a web.config file to the project and add this:
You might have to change the name of the database.
Now you need to have a model. I created a new cs file in app_code, with the name "GuestbookEntry.cs":
using System;
using System.ComponentModel.DataAnnotations;
public class GuestbookEntry
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required]
[StringLength(30)]
public string Name { get; set; }
[Required]
[StringLength(60)]
public string Email { get; set; }
[Required]
[StringLength(1000)]
public string Message { get; set; }
[Timestamp]
public DateTime Posted { get; set; }
}
This is the model that will be used by Entity Framework 4.
Now we need a DataContext. I created a DataContext.cs file and added this:
using System.Data.Entity;
public class DataContext : DbContext
{
public DbSet<GuestbookEntry> Guestbook { get; set; }
}
To work with the data easily, I created a repository (GuestbookRepository.cs):
using System;
using System.Collections.Generic;
using System.Linq;
public class GuestbookRepository
{
private DataContext _ctx = new DataContext();
public GuestbookEntry First(Func<GuestbookEntry, bool> where)
{
return _ctx.Guestbook.First(where);
}
public List<GuestbookEntry> GetAll()
{
return _ctx.Guestbook.ToList();
}
public void Add(GuestbookEntry entity)
{
entity.Posted = DateTime.Now;
_ctx.Guestbook.Add(entity);
_ctx.SaveChanges();
}
public void Delete(GuestbookEntry entity)
{
_ctx.Guestbook.Remove(entity);
_ctx.SaveChanges();
}
}
The last step before we are done is to make sure the database is recreated after we change the model. I created a _start.cshtml, which will be executed every time the site is started (like Application_Start in global.asax). It looks like this:
@using db = System.Data.Entity.Infrastructure;
@{
db.Database.SetInitializer<DataContext>(new db.RecreateDatabaseIfModelChanges<DataContext>());
}
Now we are done! To test this, add this to default.cshtml:
@{
var gb = new GuestbookRepository();
if (IsPost)
{
var entry = new GuestbookEntry()
{
Name = "Mikael",
Email = "my@email.com",
Message = "New message!"
};
gb.Add(entry);
}
var grid = new WebGrid(gb.GetAll());
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
@grid.GetHtml()
<form action="/" method="post">
<input type="submit" value="Add post" />
</form>
</body>
</html>
When you click on the button, a new post will be created and then displayed using the WebGrid helper.
Mikael Söderström
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Marked as answer by zigomir on Jul 19, 2010 12:06 PM
I'm reading scottgu's post on EF Code-First: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
He's writing about NerdDinnersIntializer, which is used to fill database with records, so that database isn't empty every time you change a model. But this isn't working for me. In a _start.cshtml I use this:
using System;
using System.Data.Entity.Infrastructure;
using System.Collections.Generic;
namespace si.caha.Model
{
public class DataInitializer : RecreateDatabaseIfModelChanges<DataContext>
{
protected override void Seed(DataContext context)
{
var items = new List<Item>
{
new Item { Title = "Item 1" },
new Item { Title = "Item 2" }
};
items.ForEach(d => context.Items.Add(d));
}
}
}
zigomir
Member
38 Points
35 Posts
Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 19, 2010 10:42 AM|LINK
Kinda asking everything in a subject of this post. I'm wondering if it is possible to include Microsoft.Data.Entity.CTP.dll reference to WebMatrix site and if the Compact edition of the database would support it? I'd kinda love use something like code-first development in my WebMatrix project. I think is even easier than writing SQL-s into the code...
Also I'm reading that Linq isn't supported yet and I'd really like to know if it's planed to include it?
entity framework webmatrix linq code first
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 19, 2010 11:33 AM|LINK
I haven´t tested this myself, but it should work since ASP.NET Web Pages is built on .NET 4.0.
I will try this right away, be right back!
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 19, 2010 12:03 PM|LINK
Cool! It works!
This is how to do it:
First you need to add Microsoft.Data.Entity.CTP.dll in ~/bin. Then create a database if you don´t already have one.After that you add a web.config file to the project and add this:
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="DataContext" connectionString="Data Source=|DataDirectory|EF4.sdf" providerName="System.Data.SqlServerCe.4.0" /> </connectionStrings> <system.web> <compilation debug="false" targetFramework="4.0" /> </system.web> </configuration>You might have to change the name of the database.
Now you need to have a model. I created a new cs file in app_code, with the name "GuestbookEntry.cs":
using System; using System.ComponentModel.DataAnnotations; public class GuestbookEntry { [ScaffoldColumn(false)] public int Id { get; set; } [Required] [StringLength(30)] public string Name { get; set; } [Required] [StringLength(60)] public string Email { get; set; } [Required] [StringLength(1000)] public string Message { get; set; } [Timestamp] public DateTime Posted { get; set; } }This is the model that will be used by Entity Framework 4.
Now we need a DataContext. I created a DataContext.cs file and added this:
using System.Data.Entity; public class DataContext : DbContext { public DbSet<GuestbookEntry> Guestbook { get; set; } }To work with the data easily, I created a repository (GuestbookRepository.cs):
using System; using System.Collections.Generic; using System.Linq; public class GuestbookRepository { private DataContext _ctx = new DataContext(); public GuestbookEntry First(Func<GuestbookEntry, bool> where) { return _ctx.Guestbook.First(where); } public List<GuestbookEntry> GetAll() { return _ctx.Guestbook.ToList(); } public void Add(GuestbookEntry entity) { entity.Posted = DateTime.Now; _ctx.Guestbook.Add(entity); _ctx.SaveChanges(); } public void Delete(GuestbookEntry entity) { _ctx.Guestbook.Remove(entity); _ctx.SaveChanges(); } }The last step before we are done is to make sure the database is recreated after we change the model. I created a _start.cshtml, which will be executed every time the site is started (like Application_Start in global.asax). It looks like this:
@using db = System.Data.Entity.Infrastructure; @{ db.Database.SetInitializer<DataContext>(new db.RecreateDatabaseIfModelChanges<DataContext>()); }Now we are done! To test this, add this to default.cshtml:
@{ var gb = new GuestbookRepository(); if (IsPost) { var entry = new GuestbookEntry() { Name = "Mikael", Email = "my@email.com", Message = "New message!" }; gb.Add(entry); } var grid = new WebGrid(gb.GetAll()); } <!DOCTYPE html> <html> <head> <title></title> </head> <body> @grid.GetHtml() <form action="/" method="post"> <input type="submit" value="Add post" /> </form> </body> </html>When you click on the button, a new post will be created and then displayed using the WebGrid helper.
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
zigomir
Member
38 Points
35 Posts
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 19, 2010 12:07 PM|LINK
Wow, thank you very much! :)
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 19, 2010 12:09 PM|LINK
If you want to edit the data, you can use the built-in support for SQL Server Compact in WebMatrix. Works great!
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
zigomir
Member
38 Points
35 Posts
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 20, 2010 08:48 AM|LINK
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">I have one more question. </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">I'm reading scottgu's post on EF Code-First: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">He's writing about NerdDinnersIntializer, which is used to fill database with records, so that database isn't empty every time you change a model. But this isn't working for me. In a _start.cshtml I use this:</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">db.Database.SetInitializer<DataContext>(new DataInitializer());</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">DataInitializer class:</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System.Data.Entity.Infrastructure;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System.Collections.Generic;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">namespace si.caha.Model</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">{</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public class DataInitializer : RecreateDatabaseIfModelChanges<DataContext></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> protected override void Seed(DataContext context)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> var items = new List<Item></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> new Item { Title = "Item 1" },</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> new Item { Title = "Item 2" }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> };</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> items.ForEach(d => context.Items.Add(d));</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">}</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">But database table Items is still empty. </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Also I'm using WebSecurity components for authentication and I'd love to use data initialization for admin user, so I'd always have it in my database, not needing to registering every time I change a model.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Thank's in advance!</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div>I have one more question.
I'm reading scottgu's post on EF Code-First: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
He's writing about NerdDinnersIntializer, which is used to fill database with records, so that database isn't empty every time you change a model. But this isn't working for me. In a _start.cshtml I use this:
using System; using System.Data.Entity.Infrastructure; using System.Collections.Generic; namespace si.caha.Model { public class DataInitializer : RecreateDatabaseIfModelChanges<DataContext> { protected override void Seed(DataContext context) { var items = new List<Item> { new Item { Title = "Item 1" }, new Item { Title = "Item 2" } }; items.ForEach(d => context.Items.Add(d)); } } }But database table Items is still empty.
Thank's in advance!
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 20, 2010 09:39 AM|LINK
What if you recreate the database in for example "createdb.cshtml" and try to visit that page?
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
zigomir
Member
38 Points
35 Posts
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 20, 2010 09:53 AM|LINK
This isn't working neither...
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 20, 2010 09:57 AM|LINK
Do you get any error messages?
If it doesn´t work, you could just add them normally like in my sample code after the database is recreated.
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
zigomir
Member
38 Points
35 Posts
Re: Entity Framework 4 and WebMatrix - is it or will it be possible?
Jul 20, 2010 11:55 AM|LINK
No error messages. I've used your sample code for now, tnx.