مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

مهیار

عضویت از 1394/10/10

ساخت دیتا بیس از طریق Code first ..??!

  • سه شنبه 5 مرداد 1395
  • 19:29
تشکر میکنم

سلام دوستان من میخواستم یک دیتا بیس از طریق 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; }
        }

 

 

 

پاسخ های این پرسش

تعداد پاسخ ها : 3 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • سه شنبه 5 مرداد 1395
  • 20:32

در () داخل base به شکل زیر بنویسید

:base("SchoolContext")

 

کاربر سایت

مهیار

عضویت از 1394/10/10

  • چهارشنبه 6 مرداد 1395
  • 22:26

باز هم مشکل میگیره

 

 

 

احتمال میدم از Appconfig باشه ..

ارور ملاحظه کنین ..



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: 

اینم Appconfig  هست : بنظرتون کانکش استریگ درسته اخه خودم وارد کردم



<?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>
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • پنجشنبه 7 مرداد 1395
  • 08:56

ممکنه از رشته اتصال باشه

اسم بانک اطلاعاتیتون SchoolContext هست ؟

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)