我有一本教程书,首先使用代码构建MVC API应用程序。我想先使用DB。当我对api运行调用时,它在内部异常中告诉我它找不到“dbo”。我怀疑.dbo是问题,如果它被删除(或者如果我在某处预先设置)它会工作,但我看不到在哪里改变代码来做到这一点。
以下是我得到的整个错误,上面提到的内容在$ id中引用:“3”
$ id:“1”,消息:“发生错误。”,ExceptionMessage:“The “ObjectContent
1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.", ExceptionType: "System.InvalidOperationException", StackTrace: null, InnerException: { $id: "2", Message: "An error has occurred.", ExceptionMessage: "An error occurred while executing the command definition. See the inner exception for details.", ExceptionType: "System.Data.Entity.Core.EntityCommandExecutionException", StackTrace: " at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) at System.Data.Entity.Core.Objects.ObjectQuery
1<> c__DisplayClass7.b__6() 在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T](Func键1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery
1<> c__DisplayClass7.b__5() 在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func键1 operation) at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(可空1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator&GT; b__0() 在System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()", InnerException: { $id: "3", Message: "An error has occurred.", **ExceptionMessage: "Invalid object name 'dbo.ContactType'."**, ExceptionType: "System.Data.SqlClient.SqlException", StackTrace: " at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)at System.Data.SqlClient.SqlInternalConnection.OnError(SQLEXCEPTION exception,Boolean breakConnection,Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource
1 completion,Int32 timeout,Task&amp;任务, 布尔和放大器; usedCache,Boolean asyncWrite,Boolean inRetry)at System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法)at System.Data.SqlClient.SqlCommand.ExecuteReader(的CommandBehavior 行为,String方法)at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(的CommandBehavior 行为) System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) 在 System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b__c(的DbCommand t,DbCommandInterceptionContext1 c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher
1.Dispatch [TTarget,TInterceptionContext,TResult](TTarget target,Func3 operation, TInterceptionContext interceptionContext, Action
3执行,Action`3执行)at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(的DbCommand 命令,DbCommandInterceptionContext interceptionContext)at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(的CommandBehavior 行为) System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) 在 System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)“
DBContext.edmx
<EntityType Name="ContactType">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="Description" Type="varchar" MaxLength="50" Nullable="false" />
</EntityType>
DBContext.Context.cs
public virtual DbSet<ContactType> ContactType { get; set; }
public virtual int addContact(Nullable<int> contactTypeID, string emailAddress, string lastName, string firstName, string companyName, string phoneNumber1, string phoneNumber2, string comment)
{
var contactTypeIDParameter = contactTypeID.HasValue ?
new ObjectParameter("ContactTypeID", contactTypeID) :
new ObjectParameter("ContactTypeID", typeof(int));
var emailAddressParameter = emailAddress != null ?
new ObjectParameter("EmailAddress", emailAddress) :
new ObjectParameter("EmailAddress", typeof(string));
var lastNameParameter = lastName != null ?
new ObjectParameter("LastName", lastName) :
new ObjectParameter("LastName", typeof(string));
var firstNameParameter = firstName != null ?
new ObjectParameter("FirstName", firstName) :
new ObjectParameter("FirstName", typeof(string));
var companyNameParameter = companyName != null ?
new ObjectParameter("CompanyName", companyName) :
new ObjectParameter("CompanyName", typeof(string));
var phoneNumber1Parameter = phoneNumber1 != null ?
new ObjectParameter("PhoneNumber1", phoneNumber1) :
new ObjectParameter("PhoneNumber1", typeof(string));
var phoneNumber2Parameter = phoneNumber2 != null ?
new ObjectParameter("PhoneNumber2", phoneNumber2) :
new ObjectParameter("PhoneNumber2", typeof(string));
var commentParameter = comment != null ?
new ObjectParameter("Comment", comment) :
new ObjectParameter("Comment", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("addContact", contactTypeIDParameter, emailAddressParameter, lastNameParameter, firstNameParameter, companyNameParameter, phoneNumber1Parameter, phoneNumber2Parameter, commentParameter);
}
ContactType.cs
namespace AHT.Data
{
using System;
using System.Collections.Generic;
public partial class ContactType
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ContactType()
{
this.Contact = new HashSet<Contact>();
}
public int ID { get; set; }
public string Description { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Contact> Contact { get; set; }
}
}
答案 0 :(得分:0)
请忽略,这是一个愚蠢的配置字符串错误。