2

我需要在 ASP.net MVC3 环境中通过实体框架处理 Advantage Database Server(ADS Ver.9.1)。

我已经为此花了将近 2 周的时间 :( 但我仍然找不到答案。

为了获得正确的建议,我将尝试一步一步地展示我是如何以及我做了什么。所以如果你发现我犯了一些错误,或者你知道我可以尝试什么,请给我建议。请..

首先,我在 ADS 中创建了一个 TEST 表。

CREATE TABLE TEST ( 
      ID AutoInc,
      Name CIChar( 50 )) IN DATABASE;

并将 ID 定义为主键。

在此处输入图像描述

我尝试使用控制台应用程序访问此表。

使用 Visual Studio 2010,创建新的控制台应用程序 (C#)

我这样打字

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Advantage.Data.Provider;

namespace Ads_Console_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            AdsConnection conn = new AdsConnection("Data Source=\\\\10.0.111.4:6262\\20090622\\SHK\\SHK.ADD;ServerType=REMOTE;User ID=AdsSys;Password=XXXXX;");
            AdsCommand cmd;
            AdsDataReader reader;
            int iField;

            try
            {
                // make the connection to the server
                conn.Open();

                // create a command object
                cmd = conn.CreateCommand();

                // specify a simple SELECT statement
                cmd.CommandText = "select * from TEST";

                // execute the statement and create a reader
                reader = cmd.ExecuteReader();

                // dump the results of the query to the console
                while (reader.Read())
                {
                    for (iField = 0; iField < reader.FieldCount; iField++)
                        Console.Write(reader.GetValue(iField) + " ");
                    Console.WriteLine();
                }

                // close the reader (can’t execute other statements
                // on this command or connection) until it is closed
                reader.Close();
                conn.Close();
            }
            catch (AdsException e)
            {
                // print the exception message
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();    
        }
    }
}

并运行,然后它成功连接到 ADS 并获取数据。

在此处输入图像描述

如您所见,该表有 3 行。

现在,

我将尝试使用实体框架访问 ADS。(MVC3)

我使用 MVC3 框架创建新项目。并选择空模板。

我使用 MVC3 框架创建新项目。 并选择空模板。

并将 Advantage.Data.Provider 添加到 Reference。

在此处输入图像描述

接下来,我修改了 Web.Config(root) 文件,添加了 connectionStrings。

<connectionStrings>
    <add name="EFAdsContext" connectionString="Data Source=\\10.0.111.4:6262\20090622\SHK\SHK.ADD;ServerType=REMOTE;User ID=adsSys;Password=XXXXX;
         TrimTrailingSpaces=True;" providerName="Advantage.Data.Provider" />
  </connectionStrings>

而且,我在 Models 文件夹中创建了 3 个类。

  1. EFAdsContext.cs
  2. 测试存储库.cs
  3. 测试.cs

这是代码,

1. EFAdsContext.cs

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;


    namespace Ads_test_mvc3.Models
    {
        public class EFAdsContext : DbContext
        {
            public DbSet<TEST> TEST { get; set; }
        }
    }

2.TestRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ads_test_mvc3.Models
{
    public class TestRepository
    {
        private EFAdsContext context = new EFAdsContext();

        public IQueryable<TEST> test
        {
            get { return context.TEST; }
        }
    }
}

3. 测试.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Ads_test_mvc3.Models
{
    public class TEST
    {
        [Key]
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

并将 HomeControllers.cs 创建到 Controllers 文件夹中。

HomeControllers.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ads_test_mvc3.Models;

namespace Ads_test_mvc3.Controllers
{
    public class HomeController : Controller
    {
        public ViewResult Index()
        {

            TestRepository tests = new TestRepository();
            int cnt = tests.test.Count();


            ViewBag.cnt = cnt;
            return View();
        }

    }
}

最后为 HomeController 添加视图

/Views/Home/Index.cshtml

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


Cnt : @ViewBag.cnt

并运行这个应用程序,

在此处输入图像描述

然后,我收到此错误消息。=3

我知道这是一篇很长的文章,但我尽量写得尽可能详细。

如果有人知道如何解决这个问题,请告诉我。

谢谢!

[堆栈跟踪]

[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) +1150
   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) +58
   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
   Ads_test_mvc3.Controllers.HomeController.Index() in C:\Users\mark\Documents\Visual Studio 2010\Projects\Ads_test_mvc3\Ads_test_mvc3\Controllers\HomeController.cs:20
   lambda_method(Closure , ControllerBase , Object[] ) +62
   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
4

0 回答 0