سلام دوستان من میخواستم یک دیتا بیس از طریق Code first ایجاد کنم ..
اما نمیدونم مشکلم کجاست ؟؟! و چرا به اروو میخوردم ؟؟ من کانکشن استیرنگ هم ب صورت زیر تعریف کردم .. دوستان میتونن راهنمایی کنن؟؟
ویا یک نمونه میخواستم دیتا بیس از طریق Code first برام بزارین ..
<connectionStrings> <add name="SchoolContext" connectionString="Data Source=.;Initial Catalog=SchoolContext;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
static void Main(string[] args) { var ctx = new SchoolContext(); Student stud = new Student() { StudentName = "New Student" }; ctx.Students.Add(stud); ctx.SaveChanges(); } public class SchoolContext : DbContext { public SchoolContext(): base() { } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } } public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } public DateTime DateOfBirth { get; set; } public byte[] Photo { get; set; } public decimal Height { get; set; } public float Weight { get; set; } public Teacher Teacher { get; set; } public Standard Standard { get; set; } } public class Standard { public Standard() { } public int StandardId { get; set; } public string StandardName { get; set; } public ICollection<Student> Students { get; set; } } public class Teacher { public Teacher() { } public int TeacherId { get; set; } public string TeacherName { get; set; } }
در () داخل base به شکل زیر بنویسید
:base("SchoolContext")
System.TypeInitializationException was unhandled HResult=-2146233036 Message=The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. Source=EntityFramework TypeName=System.Data.Entity.Internal.AppConfig StackTrace: at System.Data.Entity.Internal.LazyInternalConnection..ctor(DbContext context, String nameOrConnectionString) at System.Data.Entity.DbContext..ctor(String nameOrConnectionString) at CodeFirst_01.Program.SchoolContext..ctor() in D:\New Project\CodeFirst_01\CodeFirst_01\Program.cs:line 25 at CodeFirst_01.Program.Main(String[] args) in D:\New Project\CodeFirst_01\CodeFirst_01\Program.cs:line 14 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: BareMessage=Configuration system failed to initialize HResult=-2146232062 Line=0 Message=Configuration system failed to initialize Source=System.Configuration StackTrace: at System.Configuration.ConfigurationManager.PrepareConfigSystem() at System.Configuration.ConfigurationManager.GetSection(String sectionName) at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName) at System.Diagnostics.DiagnosticsConfiguration.Initialize() at System.Diagnostics.DiagnosticsConfiguration.get_IndentSize() at System.Diagnostics.TraceInternal.InitializeSettings() at System.Diagnostics.TraceInternal.get_Listeners() InnerException: BareMessage=Unrecognized configuration section configuration. Filename=D:\New Project\CodeFirst_01\CodeFirst_01\bin\Debug\CodeFirst_01.vshost.exe.Config HResult=-2146232062 Line=7 Message=Unrecognized configuration section configuration. (D:\New Project\CodeFirst_01\CodeFirst_01\bin\Debug\CodeFirst_01.vshost.exe.Config line 7) Source=System.Configuration StackTrace: at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal) at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors) at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors() at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey) InnerException:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <configuration> </configuration> <connectionStrings> <add name="SchoolContext" connectionString="Data Source=.;Initial Catalog=SchoolContext;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration>
ممکنه از رشته اتصال باشه
اسم بانک اطلاعاتیتون SchoolContext هست ؟
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)