如何在ASP.net MVC3中获取Advantage数据库数据?

时间:2012-04-03 19:07:52

标签: asp.net asp.net-mvc asp.net-mvc-3 entity-framework advantage-database-server

我在Asp.net MVC3环境中,并使用Advantage Database 9.1。

我想在Asp.net(+ Repository模式)中获取Advantage Database数据

我的优势数据库有WebOrderHd表,我想获取表的PK字段并计算它。

(表名:WebOrderHd字段名称:PK(字符(36))

我认为我已成功连接到数据库服务器,但无法获取数据。

这是我的代码,

的Web.config

<add name="EFAdsContext" connectionString="Data Source=\\XXX.XX.XX.XX:6262\DB_BACK\SD.ADD;ServerType=REMOTE;User ID=AdsSys;Password=xxxxx"
         providerName="Advantage.Data.Provider" />

/Concrete/EFAdsContext.cs

public class EFAdsContext : DbContext
{
    public DbSet<WebOrderHd> weborderhd { get; set; }
}

/Concrete/EFWebOrderHdRepository.cs

public class EFWebOrderHdRepository : IWebOrderHdRepository
{
    private EFAdsContext context = new EFAdsContext();

    public IQueryable<WebOrderHd> WebOrderHd
    {
        get { return context.weborderhd; }
    }
}

/Abstract/IWebOrderHdRepository.cs

public interface IXcartOrdersRepository
{
    IQueryable<XcartOrder> XcartOrders { get; }
}

/Entities/WebOrderHd.cs

public class WebOrderHd
{
    [Key]
    public string PK { get; set; }
    //public string ORDERNO { get; set; }
}

在我的控制器中,

IWebOrderHdRepository WebOrderHdRepository = new EFWebOrderHdRepository();
int adsOrderCnt = WebOrderHdRepository.WebOrderHd.Count();

在代码上方运行时,会发生错误:

字典中没有给定的密钥。

描述:在执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。

异常详细信息: System.Collections.Generic.KeyNotFoundException:字典中没有给定的密钥。

我猜错了消息说Asp.net在WebOrderHd表中找不到PK字段, 但我确信有PK领域。

有人知道我做错了吗?

谢谢!

堆栈追踪:

[KeyNotFoundException: The given key was not present in the dictionary.]
   System.Collections.Generic.Dictionary`2.get_Item(TKey key) +9619597
   Advantage.Data.Provider.AdsProviderManifest.GetStoreType(TypeUsage edmType) +2930
   System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.MapTableColumn(EdmProperty property, DbTableColumnMetadata tableColumnMetadata, Boolean isInstancePropertyOnDerivedType, Boolean isKeyProperty) +60
   System.Data.Entity.ModelConfiguration.Edm.Services.PropertyMappingGenerator.Generate(EdmEntityType entityType, IEnumerable`1 properties, DbEntitySetMapping entitySetMapping, DbEntityTypeMappingFragment entityTypeMappingFragment, IList`1 propertyPath, Boolean createNewColumn) +1293
   System.Data.Entity.ModelConfiguration.Edm.Services.EntityTypeMappingGenerator.Generate(EdmEntityType entityType, DbDatabaseMapping databaseMapping) +496
   System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(EdmModel model, DbDatabaseMapping databaseMapping) +122
   System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel model) +30
   System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +189
   System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +59
   System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +62
   System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +117
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +453
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +57
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
   System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +37
   System.Linq.Queryable.Count(IQueryable`1 source) +50
   SportsStore.WebUI.Controllers.ProductController.List(String category, Int32 page) in C:\Users\mark\documents\visual studio 2010\Projects\SportsStore\SportsStore.WebUI\Controllers\ProductController.cs:37
   lambda_method(Closure , ControllerBase , Object[] ) +151
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969117
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

表格结构

CREATE TABLE WEBORDERHD ( 
      PICKNO Char( 9 ),
      STATION Char( 10 ),
      CUSTNO Char( 32 ),
      COMPANY Char( 35 ),
      BNAME Char( 255 ),
      ADDRESS1 Char( 30 ),
      ADDRESS2 Char( 30 ),
      CITY Char( 64 ),
      STATE Char( 2 ),
      ZIP Char( 10 ),
      SCOMPANY Char( 35 ),
      SNAME Char( 255 ),
      SADDRESS1 Char( 30 ),
      SADDRESS2 Char( 30 ),
      SCITY Char( 64 ),
      SSTATE Char( 2 ),
      SZIP Char( 10 ),
      SALESPN Char( 10 ),
      TAXRATE Numeric( 6 ,3 ),
      CERTIFICA Char( 10 ),
      TOTAL Numeric( 11 ,2 ),
      CONTACT Char( 15 ),
      TITLE Char( 10 ),
      PHONE Char( 17 ),
      FAX Char( 17 ),
      BEEPER Char( 17 ),
      FREIGHT Numeric( 11 ,2 ),
      FINVNO Char( 6 ),
      CUSTNOTE Memo,
      MEMO Memo,
      MODDATE Date,
      PRNDATE Date,
      SHIPVIA Char( 15 ),
      SHIPDATE Date,
      DATE Date,
      STATUS Char( 30 ),
      EMAIL Char( 128 ),
      TAX Numeric( 11 ,2 ),
      PAYTYPE Char( 20 ),
      PAYREF Char( 20 ),
      CCREF Char( 50 ),
      PK Char( 36 ),
      ORDTIME Char( 8 ),
      SHIPMETHOD Char( 30 ),
      PROCSTATUS Char( 20 ),
      CUSTOMERPONO Char( 50 ),
      CUSTOMERJOBNO Char( 50 ),
      CUSTOMERPOCONTACT Char( 50 ),
      CUSTOMERPOCOMMENT Memo,
      S_COUNTRY Char( 10 ),
      B_COUNTRY Char( 10 ),
      SHIPBRANCH Char( 3 )) IN DATABASE;

1 个答案:

答案 0 :(得分:0)

我绝不是EF的专家,但是......你试过让PK属性虚拟吗?我的理解是EF需要使用模型创建代理,因此它们必须是虚拟的,以便它可以按照需要的方式实现它。

[Key]    
public virtual string PK { get; set; }

此外,我建议首先尝试访问没有高级模式的数据,这样您就可以验证基础工作。我总是在那里添加一个单独的控制台项目并测试一些内容,因为您不必创建整个页面来查看结果。