简体   繁体   中英

How to get the Advantage Database data in ASP.net MVC3?

I'm in Asp.net MVC3 environment, and I use Advantage Database 9.1.

and I want to get the Advantage Database data in Asp.net (+Repository pattern)

My Advantage Database has WebOrderHd table and I want to get the PK field of the table and count it.

(Table Name : WebOrderHd Field Name : PK (character(36))

I think I connect to DB server successfully but I can not get the data.

here is my codes,

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; }
}

And In my controller ,

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

When run above code, an error occur :

The given key was not present in the dictionary.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

I guessed the error message say Asp.net can not find the PK field in WebOrderHd table, but I am sure there is PK field.

Anybody know what I am doing wrong?

Thank You!

Stack Trace :

[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

Table Structure

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;

I by no means am an expert at EF but... have you tried making the PK property virtual? My understanding is EF needs to create a proxy with your models so they have to be virtual so it can implement it the way it needs.

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

Also, I would recommend trying to access the data without an advanced pattern first that way you can verify the basics work. I always add a separate console project and test things in there as you don't have to create a whole page just to see the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM