我正在用f#构建一个Web API来测试一些东西。我有c#的背景,我已经组织了我在那里做的事情。
你可以在这里找到repo: https://github.com/MrGodlike6/WebApiTest
我在WebApiTest中引用了WebApiTest. repository . sqlserver项目。WebApiTest.Repositories.SqlServer命名空间
事情是解决方案编译得很好,我可以运行应用程序,只是我没有智能感知来自SqlServer项目的任何东西(例如在CompositionRoot模块)。
我尝试了很多东西,从不同的DB,为DB项目做一个。net Framework 4.7.2项目。到目前为止还没有任何效果。Nuget &Paket和没有区别。尝试VS 2019。在VS 2022中,它给出了一些无关的编译错误。
我使用JetBrains的dotPeek来查看生成的.dll和命名空间&模块如预期的那样存在。不知道是什么原因造成的
你还能指出我使用的项目/文件夹结构是否适合f#吗?如有其他建议也欢迎。
编辑:SQL部分基于https://github.com/isaacabraham/get-programming-fsharp/tree/master/src/code-listings/lesson-35。虽然SQL代码没有放在单独的项目中,但这里的一切都可以正常工作。
我已经评论了SqlEnumProvider,我不再有组合根的问题。我将仔细研究一下这个提供者,看看是否能找到一些东西。
EDIT2:在GitHub上创建了一个问题:https://github.com/fsprojects/FSharp.Data.SqlClient/issues/410
我已经复制了你所看到的。这个问题似乎是由FSharp的设计时错误引起的。数据类型提供程序。特别是,SqlEnumProvider
似乎是源。当我从accountrerepository中删除DbOperations
类型时。fs(使用普通字符串而不是enum值),我能够在CompositionRoot.fs中获得WebApiTest.Repositories.SqlServer
的智能感知。
这是我的笨拙的解决方案:
[<AutoOpen>]
module private DB =
let [<Literal>] Conn = @"Data Source=.;Database=BankAccountDb;Integrated Security=True;Connect Timeout=60"
type AccountsDb = SqlProgrammabilityProvider<Conn>
type GetAccountId = SqlCommandProvider<"SELECT TOP 1 AccountId FROM dbo.Account WHERE Owner = @owner", Conn, SingleRow = true>
type FindTransactions = SqlCommandProvider<"SELECT Timestamp, OperationId, Amount FROM dbo.AccountTransaction WHERE AccountId = @accountId", Conn>
type FindTransactionsByOwner = SqlCommandProvider<"SELECT a.AccountId, at.Timestamp, at.OperationId, at.Amount FROM dbo.Account a LEFT JOIN dbo.AccountTransaction at on a.AccountId = at.AccountId WHERE Owner = @owner", Conn>
// type DbOperations = SqlEnumProvider<"SELECT Description, OperationId FROM dbo.Operation", Conn>
let toBankOperation operationId =
match operationId with
| "Deposit" -> Deposit
| "Withdraw" -> Withdraw
| _ -> failwith "Unknown operation!"
let fromBankOperation bankOperation =
match bankOperation with
| Deposit -> "Deposit"
| Withdraw -> "Withdraw"
我在GitHub上搜索了一个现有的问题,但没有找到一个,所以你可能想自己打开一个新的。
如果有人想试试这个,这里是我用来编译东西的虚拟数据库模式:
USE [BankAccountDb]
GO
/****** Object: Table [dbo].[Account] Script Date: 9/16/2021 11:40:48 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Account](
[Owner] [nchar](10) NOT NULL,
[AccountId] [uniqueidentifier] NOT NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[AccountTransaction] Script Date: 9/16/2021 11:40:48 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AccountTransaction](
[AccountId] [uniqueidentifier] NOT NULL,
[Timestamp] [datetime] NOT NULL,
[OperationId] [nchar](10) NOT NULL,
[Amount] [money] NOT NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Operation] Script Date: 9/16/2021 11:40:48 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Operation](
[Description] [nvarchar](100) NULL,
[OperationId] [nvarchar](100) NULL
) ON [PRIMARY]
GO
谢谢Brian的剧本。
如文档所述,
"枚举类型(或枚举类型)是由底层整型数值类型的一组命名常量定义的值类型
我将dbo.Operation
和dbo.AccountTransaction
中的OperationId
列数据类型更改为int,并填充表dbo。操作,因为SqlEnumProvider
在编译过程中抱怨缺少数据。
CREATE TABLE [dbo].[Operation] (
[Description] NVARCHAR (100) NULL,
[OperationId] INT NOT NULL
);
CREATE TABLE [dbo].[AccountTransaction] (
[AccountId] UNIQUEIDENTIFIER NOT NULL,
[Timestamp] DATETIME NOT NULL,
[OperationId] INT NOT NULL,
[Amount] MONEY NOT NULL
);
insert dbo.Operation (Description, OperationId) values ('Deposit', 1), ('Withdraw', 2)
现在我可以编译解决方案了。