Näin kirjoitti Markku Kuuselo Usenetin ryhmään sfnet.atk.ohjelmointi:
> ASP.NET-sovellus toimii muuten hyvin, mutta tekemäni testiaineisto ei
> tallennu tietokantaan. Tyhjiä tauluja vain näkyy.
> Missä vika?
Täydennyksenä asiaan liittyvä koodi.
Sampledata.cs eli testitietojen luonti:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcFishingDiary_MKuuselo.Models
{
public class SampleData :
DropCreateDatabaseIfModelChanges<FishingDiaryEntities>
{
protected override void Seed(FishingDiaryEntities context)
{
var fEvents = new List<FishingEvent> //Isätaulut oliolla
{
new FishingEvent
{
Place = "Kiiminkijoki",
Date = DateTime.Parse("5.7.2011"),
Temperature = 20,
Weather = "Puolipilvistä"
},
new FishingEvent
{
Place = "Kuusinkijoki",
Date = DateTime.Parse("10.7.2011"),
Temperature = 16,
Weather = "Tihkusadetta"
},
new FishingEvent
{
Place = "Oulujärvi",
Date = DateTime.Parse("12.7.2011"),
Temperature = 25,
Weather = "Aurinkoista"
},
new FishingEvent
{
Place = "Kiiminkijoki",
Date = DateTime.Parse("3.8.2011"),
Temperature = 15,
Weather = "Sadetta"
},
new FishingEvent
{
Place = "Hailuoto",
Date = DateTime.Parse("10.8.2011"),
Temperature = 20,
Weather = "Puolipilvistä"
}
};
var species = new List<Species>
{
new Species {SpeciesName = "Lohi", Description = "Kalojen kuningas"},
new Species {SpeciesName = "Nieriä", Description = "No nieriähän se"},
new Species {SpeciesName = "Siika", Description = "Paras suolakala"},
new Species {SpeciesName = "Kuha", Description = "Oulujärven antia"}
};
new List<MyCatch> //Lapsitaulut listan avulla
{
new MyCatch
{
Species = species.Single(s => s.SpeciesName == "Siika"),
Weight = 15,
Lenght = 80,
FishingEvent = fEvents.Single(f => f.Place == "Hailuoto")
},
new MyCatch
{
Species = species.Single(s => s.SpeciesName == "Siika"),
Weight = 3,
Lenght = 30,
FishingEvent = fEvents.Single(f => f.Place == "Oulujärvi")
},
new MyCatch
{
Species = species.Single(s => s.SpeciesName == "Nieriä"),
Weight = 15,
Lenght = 80,
FishingEvent = fEvents.Single(f => f.Place == "Kuusinkijoki")
},
new MyCatch
{
Species = species.Single(s => s.SpeciesName == "Lohi"),
Weight = 15,
Lenght = 80,
FishingEvent =
fEvents.Single(
f => f.Place == "Kiiminkijoki" && f.Date == DateTime.Parse("3.8.2011"))
}
}.ForEach(a => context.MyCatch.Add(a));
}
}
}
Ja sitten webconfig.cs eli tietokannan paikka:
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User
Instance=true"
providerName="System.Data.SqlClient" />
<add name="FishingDiaryEntities"
connectionString="Data
Source=|DataDirectory|MvcFishingDiary_MKuuselo.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Lopuksi global.asax.cs eli tietokannan käyttöön otto
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcFishingDiary_MKuuselo
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit
http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
}
protected void Application_Start() //tietokannan käyttöönotto
{
System.Data.Entity.Database.SetInitializer(new
MvcFishingDiary_MKuuselo.Models.SampleData());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
--
Markku Kuuselo