相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to write a really simple CRUD api using sql server but NOT USING EF .

my data class looks like this

public class DataClass : ICommanderRepo
    protected string _connectionString { get; set; }
    public DataClass(string connectionString)
        _connectionString = connectionString;
    public IEnumerable<Command> GetAppCommands()
        var commands = new List<Command>();
        using (SqlConnection con = new SqlConnection(_connectionString))
            using(SqlCommand cmd = new SqlCommand("spR_GetAllCommands",con))
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if(reader.HasRows)
                    while(reader.Read())
                        commands.Add(
                            new Command { Id = Convert.ToInt32(reader[0].ToString()), HowTo = reader[1].ToString(), Line = reader[2].ToString(), Platform = reader[3].ToString() }
        return commands;

which I think will work fine. However, I'm doing something wrong when I set up the service in my startup.cs

services.AddTransient<ICommanderRepo, DataClass>(
        opt => opt.UseSqlServer(Configuration.GetConnectionString("LocalhostConnection"))

my error is "IServiceProvider does not contain a definition for UseSqlServer"

In this case you would need to create an instance in the factory delegate

string connectionString = Configuration.GetConnectionString("LocalhostConnection");
services.AddTransient<ICommanderRepo, DataClass>(provider => 
    new DataClass(connectionString)

passing in the desired constructor arguments.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章