error in controller

sblb 1,166 Reputation points
2022-09-19T16:37:14.84+00:00

Hi,

In my application the user can put manually the value in TextBox.
This value take the form 100/20

I would like to verify that this value is not already in the column of my table.

I started to do a task in the controller to create a new list

 public class CompareEco  
    {  
       public string EcoName { get; set; }  
    }  

  private async Task<CompareEco> CompareEcoIdAsync()  
        {                            
            List<CompareEco> eco = await _context.Developers.ECO.ToList();  
             
        }  

I don't understand I received :

Error CS1061 'DbSet<Developer>' does not contain a definition for 'ECO' and no accessible 'ECO' extension method that accepts a first argument of type 'DbSet<Developer>' was found (is a using directive or assembly reference missing?)   

While ECO is defined in Developer. Have you an idea where there is a problem?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,377 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,186 Reputation points
    2022-09-19T19:49:13.11+00:00

    Similar to your many other posts, you made up programming constructs that does not exist.

    _context.Developers is a collection of developer records in the Developer table. The collection of Developers object does not have a property named ECO. An item within the collection has the ECO property. I've explained this concept to you many many times over many of your threads. From my perspective it seems like you make no effort to learn C# fundamentals.

    I don't know why I can't create the list eco with the data from the column Developers.ECO.

    I'm afraid to answer this question because, like your other threads, your proposed solution is most likely not going to work. But here goes... This is how to get a list of ECO strings.

    List<string> ECOs = _context.Developers.Select(d => d.ECO).ToList();  
    

    If you are trying to figure out if a specific ECO string in the Developer table then the syntax is...

    bool exists = _context.Developers.Any(d => d.ECO == "001/20");  
    

    I strongly recommend that you go through a few Entity Framework and LINQ tutorials. Also, learn about Collections in the C# programming guide.

    Lastly, I shared several verified code samples that show how to create a unique "001/YY" string from T-SQL to LINQ. I also created an entire soliton with CRUD operations. Out of curiosity, why are these solution not acceptable? Also, why do you keep going back to the same approach that does not work?


2 additional answers

Sort by: Most helpful
  1. sblb 1,166 Reputation points
    2022-09-19T18:55:02.487+00:00

    The definition of ECO is :

    model class Developer :

     public partial class Developer  
        {  
            public int Id { get; set; }  
            .....  
            public string ECO { get; set; }   
      
    }  
      
    

    ApplicationDBContext

     public class ApplicationDBContext : DbContext  
        {  
            public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)  
            {  
            }  
            public DbSet<Developer> Developers { get; set; }  
                     
        }  
    

    In controller

     [Route("api/[controller]")]  
        [ApiController]  
        public class DeveloperController : ControllerBase  
        {  
            private readonly ApplicationDBContext _context;  
      
            public DeveloperController(ApplicationDBContext context)  
            {  
                this._context = context;  
            }  
     private async Task<CompareEco> CompareEcoIdAsync()  
             {                            
                 List<CompareEco> eco = await _context.Developers.ECO.ToList();  
                     
             }  
      
    

    I don't know why I can't create the list eco with the data from the column Developers.ECO.

    0 comments No comments

  2. sblb 1,166 Reputation points
    2022-09-20T14:36:49.757+00:00

    If I understand correctly, you used my approach which creates the value automatically when a record is inserted

    No, I use your approach to create the Variable ECR with the counter/YY each time that is validated Edit.razor

    I have another variable ECO where the user manually enters the value and the variable is independent of the ECR variable.

    The ECO variable must have the same xxx/Year format as the ECR variable but will not be linked to the ECR variable.

    you do not understand how web sites work, C#,

    I don't understand your remark!

    You asked how to get a list of ECO

    Your proposition : List<string> ECOs = _context.Developers.Select(d => d.ECO).ToList(); is Ok ! I've implemented it!

    Have you changed your approach???

    no, I kept your approach for ECR that you already gave me an answer.

    To summarise, your proposal is OK for the variable ECO.
    I need to impose the xxx/YY format type on the user, I'm thinking of doing this with an annotation in the model class. Is this possible?
    I need to check that when the user writes the value to the interface, it is the unique value in the ECO column.

    ------------------

    Edit.razor

    <FormEdit ButtonText="Update" dev="dev"  
          OnValidSubmit="@EditDeveloper" />  
      
    @code {  
      
        [Parameter] public int developerId { get; set; }  
        Developer dev = new Developer();  
      
        protected async override Task OnParametersSetAsync()  
        {  
            dev = await http.GetFromJsonAsync<Developer>($"api/developer/{developerId}");  
        }  
      
        async Task EditDeveloper()  
        {  
            await http.PutAsJsonAsync("api/developer", dev);  
            await js.InvokeVoidAsync("alert", $"Updated Successfully!");  
            uriHelper.NavigateTo("developer");  
        }  
      
    }  
    

    DeveloperController (with your proposition for ECR)

            [HttpPost]  
            public async Task<IActionResult> Post(Developer developer)  
            {  
      
                  
                YearCount dev = await IncrementDeveloperIdAsync();  
      
                developer.Year = dev.Year;  
                developer.Count = dev.Count;  
      
                _context.Add(developer);  
                await _context.SaveChangesAsync();  
                return Ok(developer.Id);  
            }        
             
            private async Task<YearCount> IncrementDeveloperIdAsync()  
            {  
                if (!await _context.Developers.AnyAsync(d => d.Year == DateTime.Now.Year))  
                {  
                    return new YearCount() { Count = 1, Year = DateTime.Now.Year };  
                }  
      
                int max = await _context.Developers.Where(d => d.Year == DateTime.Now.Year).MaxAsync(d => d.Count) + 1;  
                return new YearCount() { Count = max, Year = DateTime.Now.Year };  
            }