我真的需要您的关注和帮助。
我使用EntityFramework CodeFirst在Unity3d中设置数据库,但不确定为什么不能使用代码从数据库读取或写入数据。
我在数据库中创建项目的代码
var summerProgramDbContext = new ProgramDbContext();
var module = new Module()
{
Id = 1,
Title = "calculus",
Description = "basic operations"
};
summerProgramDbContext.Modules.Add(module);
summerProgramDbContext.SaveChanges();
错误日志
SocketException: Could not resolve host '.'
System.Net.Dns.Error_11001 (System.String hostName) (at <2d96311935af40c7b35c6ff5c955d7e7>:0)
System.Net.Dns.GetHostByName (System.String hostName) (at <2d96311935af40c7b35c6ff5c955d7e7>:0)
System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) (at <2d96311935af40c7b35c6ff5c955d7e7>:0)
System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) (at <2d96311935af40c7b35c6ff5c955d7e7>:0)
System.Net.Sockets.UdpClient.Connect (System.String hostname, System.Int32 port) (at <2d96311935af40c7b35c6ff5c955d7e7>:0)
System.Net.Sockets.UdpClient..ctor (System.String hostname, System.Int32 port) (at <2d96311935af40c7b35c6ff5c955d7e7>:0)
System.Data.SqlClient.SqlConnection+SqlMonitorSocket..ctor (System.String ServerName, System.String InstanceName) (at <2eb4400ec839419386e2047ab09e5eae>:0)
System.Data.SqlClient.SqlConnection.DiscoverTcpPortViaSqlMonitor (System.String ServerName, System.String InstanceName) (at <2eb4400ec839419386e2047ab09e5eae>:0)
System.Data.SqlClient.SqlConnection.ParseDataSource (System.String theDataSource, System.Int32& thePort, System.String& theServerName) (at <2eb4400ec839419386e2047ab09e5eae>:0)
System.Data.SqlClient.SqlConnection.Open () (at <2eb4400ec839419386e2047ab09e5eae>:0)
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36 (System.Data.Common.DbConnection t, System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext c) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1[TInterceptor].Dispatch[TTarget,TInterceptionContext] (TTarget target, System.Action`2[T1,T2] operation, TInterceptionContext interceptionContext, System.Action`3[T1,T2,T3] executing, System.Action`3[T1,T2,T3] executed) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open (System.Data.Common.DbConnection connection, System.Data.Entity.Infrastructure.Interception.DbInterceptionContext interceptionContext) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.SqlServer.SqlProviderServices+<>c__DisplayClass33.<UsingConnection>b__32 () (at <5c51bb66ce2c412d8224b39ad2626fbe>:0)
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy+<>c__DisplayClass1.<Execute>b__0 () (at <5c51bb66ce2c412d8224b39ad2626fbe>:0)
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult] (System.Func`1[TResult] operation) (at <5c51bb66ce2c412d8224b39ad2626fbe>:0)
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute (System.Action operation) (at <5c51bb66ce2c412d8224b39ad2626fbe>:0)
System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection (System.Data.Common.DbConnection sqlConnection, System.Action`1[T] act) (at <5c51bb66ce2c412d8224b39ad2626fbe>:0)
System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection (System.Data.Common.DbConnection sqlConnection, System.Action`1[T] act) (at <5c51bb66ce2c412d8224b39ad2626fbe>:0)
System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript (System.Nullable`1[T] commandTimeout, System.Data.Common.DbConnection sqlConnection, System.String createDatabaseScript) (at <5c51bb66ce2c412d8224b39ad2626fbe>:0)
System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase (System.Data.Common.DbConnection connection, System.Nullable`1[T] commandTimeout, System.Data.Entity.Core.Metadata.Edm.StoreItemCollection storeItemCollection) (at <5c51bb66ce2c412d8224b39ad2626fbe>:0)
System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase (System.Data.Common.DbConnection connection, System.Nullable`1[T] commandTimeout, System.Data.Entity.Core.Metadata.Edm.StoreItemCollection storeItemCollection) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase () (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create (System.Data.Common.DbConnection connection) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists (System.Action mustSucceedToKeepDatabase) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists (System.Action mustSucceedToKeepDatabase) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Migrations.DbMigrator.Update (System.String targetMigration) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update () (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.DatabaseCreator.CreateDatabase (System.Data.Entity.Internal.InternalContext internalContext, System.Func`3[T1,T2,TResult] createMigrator, System.Data.Entity.Core.Objects.ObjectContext objectContext) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.InternalContext.CreateDatabase (System.Data.Entity.Core.Objects.ObjectContext objectContext, System.Data.Entity.Internal.DatabaseExistenceState existenceState) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Database.Create (System.Data.Entity.Internal.DatabaseExistenceState existenceState) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.CreateDatabaseIfNotExists`1[TContext].InitializeDatabase (TContext context) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.InternalContext+<>c__DisplayClassf`1[TContext].<CreateInitializationAction>b__e () (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.InternalContext.PerformInitializationAction (System.Action action) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization () (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4 (System.Data.Entity.Internal.InternalContext c) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.RetryAction`1[TInput].PerformAction (TInput input) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction (System.Action`1[T] action) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase () (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.InternalContext.Initialize () (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType (System.Type entityType) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.Linq.InternalSet`1[TEntity].Initialize () (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.Linq.InternalSet`1[TEntity].get_InternalContext () (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.Linq.InternalSet`1[TEntity].ActOnSet (System.Action action, System.Data.Entity.EntityState newState, System.Object entity, System.String methodName) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.Internal.Linq.InternalSet`1[TEntity].Add (System.Object entity) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
System.Data.Entity.DbSet`1[TEntity].Add (TEntity entity) (at <b7b192e5a05f4f14a14aa0d5b14aeeeb>:0)
ProgramDb.Start () (at Assets/ProgramDb.cs:33)
我创建数据库的步骤
使用数据库类创建C#脚本
公共类模块 { public int ID {get;组; } 公共字符串Title {get;组; } 公共字符串说明{组; } }
打开Visual Studio>程序包管理器控制台以安装Entity Framework并启用迁移
PM>安装程序包实体框架 PM> enable-migrations
将EntityFramework.dll和EntityFramework.SqlServer.dll从Solution \ packages \ EntityFramework.6.2.0 \ lib \ net40复制到Unity Assets \ Plugins
为数据库类创建数据库上下文(第3点)
公共类ProgramDbContext:DbContext { 公共DbSet模块{组; } }
在Package Manager控制台中添加迁移和更新数据库
PM>添加迁移DatabaseInitialSetup PM>更新数据库
Sql Server Management Studio with new database created using entity framework code first