Data annotation issue in EF - c#

HI I have following entity class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace myspace
{
class aList
{
public int Id { get; set; }
[Index("atIdIndex", IsUnique=true)]
public int atId { get; set; }
public string description { get; set; }
}
}
In visual studio on [Index("atIdIndex", IsUnique=true)] I get red line under Index and it says that
Type or namespace Index couldnot be found.
Please let me know how i can fix it Thanks
'

You need to add:
using System.ComponentModel.DataAnnotations.Schema;

Had to re install EF it worked fine after that. Thanks

Related

How to use the same libraries in all the classes inside a namespace

I'm implementing various classes in a common folder and namespace all of which need to be able to deserialize JSON, so all the classes need the
using using System.Globalization;
using System.Text.Json.Serialization;
directives. I was wondering if there is a way to make them all use said libraries rather than pasting it into every new class .cs file or writing all the classes in the same .cs file. I've tried putting the using directives inside the namespace but it doesn't carry over to the other .cs files. It might be worth mentioning I'm working on a wpf C# .net framework desktop application.
Sample class without the using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MLaRealERP.Models
{
internal class ClienteP
{
[JsonPropertyName("dni")]
public string DNI { get; set; }
[JsonPropertyName("ruc")]
public string RUC { get; set; }
[JsonPropertyName("nombres")]
public string Nombres { get; set; }
[JsonPropertyName("apellidos")]
public string Apellidos { get; set; }
[JsonPropertyName("celular")]
public string Celular { get; set; }
}
If you're using C# 10 you can use the global modifier on the using directive but this is equivalent to adding it to every file in the project. See the docs here.
If you're on an earlier version, short answer is no.

Error CS0103 The name 'db' does not exist in the current context

I'm assuming I'm missing a namespace declared on the top of my controller but I tried with 'using WebMatrix.Data;' and 'using System.IO;' that they are the usual suggestions for this error. It didn't work.
I'm trying to display more than one database table in the same view and this error is stopping me. Help please.
My Controller:
using System.Linq;
using System.Web.Mvc;
using KMS.Models;
using WebMatrix.Data;
using System.IO;
namespace KMS.Controllers
{
public class KMSController : Controller{
public ActionResult Index()
{
KMSConection cs = new KMSConection();
cs.Areas = (from o in db.Areas select o).Tolist();
cs.AreaTypes = (from o in db.AreaTypes select or).Tolist();
return View(cs);
}
}
}
My ViewModel Class:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using KMS.Models;
namespace KMS.Models
{
public class KMSConection: ApplicationDbContext
{
public DbSet<Area> Areas { get; set; }
public DbSet<AreaType> AreaTypes { get; set; }
}
}
Thanks!
Because you are not mentioning the db variable you have created into your KMSController. Try this code to fix the error:
using System.Linq;
using System.Web.Mvc;
using KMS.Models;
using WebMatrix.Data;
using System.IO;
namespace KMS.Controllers
{
public class KMSController : Controller{
public ActionResult Index()
{
//Insert the actual entity referred to your db variable.
//Probably something like:
//var db = Some Database Context
KMSConection cs = new KMSConection();
cs.Areas = (from o in db.Areas select o).Tolist();
cs.AreaTypes = (from o in db.AreaTypes select or).Tolist();
return View(cs);
}
}
}

MVC error when trying to use model in view

My problem occurs when I try to tell the view which model to use. It only happens with this model though.. Here is the model code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Com.Domain.Entities
{
public class Components
{
}
public class headerModel
{
public virtual String title { get; set; }
public virtual String subtitle { get; set; }
}
}
In the view when I try to add it like this
#model Com.Domain.Entities.headerModel;
It gives me this error:
only assignment call increment decrement and new object expressions can be used as a statement
I'm not sure what's going on. Any help would be appreciated! Thanks!
Remove ; at the end of the model declaration in your view
#model Com.Domain.Entities.headerModel

TFS allowing me to access just one class out of five classes in the same folder in .net

I'm very new to working with TFS(of course not with Visual Studio). I have 6 projects in same solution and one of the projects has a folder with 6 c-sharp classes in it. When i try to access these classes from inside of other projects ( which all are in the same solution) except one class other five classes don't appear in the intellisense.
I have manually added reference to this project in other project. Which is why I'm able to access one class but my confusion is why not other classes are accessible through intellisense.
Did I do something wrong. I did check-in whatever pending changes were there in the entire solution still no luck.
Your help will be highly appreciated
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NS.GIS.PTC.Core.Entities;
namespace NS.GIS.PTC.WebServices.DataManagement.Model.Dtos
{
public class FieldDescriptionDto
{
public string Name
{
get;
set;
}
public string Type
{
get;
set;
}
public string Alias
{
get;
set;
}
public int Length
{
get;
set;
}
public bool IsSystem
{
get;
set;
}
public Domain Domain
{
get;
set;
}
}
}
here is the code and below is the code that is using it I have written it forcibly coz as u know its not appearing in intellisense
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NS.GIS.PTC.Core.Entities;
using NS.GIS.PTC.WebService.DataManagement.Model.Dtos;
namespace NS.GIS.PTC.WebService.DataManagement.API.Model
{
internal static class FieldDescriptionExtension
{
internal static FieldDescriptionDto ToFieldDescriptionDto(this FieldDescription fieldDescription)
{
return new FieldDescriptionDto
{
Name = fieldDescription.Name,
Type = fieldDescription.Type,
Alias= fieldDescription.Alias,
};
}
}
}
Please check if all the classes are marked Public.
Check if their namespaces have been included.
Restart VS and try.
Forcibly use them and see if you get a squiggle to auto-include namespace etc.

The type or namespace name 'Column' could not be found

I'm sure that I'm missing something simple here.
I'm trying to follow a Code First Entity Framework tutorial which tells me to use some Data Annotations.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace Model
{
public class Destination
{
public int DestinationId { get; set; }
[Required]
public string Name { get; set; }
public string Country { get; set; }
[MaxLength(500)]
public string Description { get; set; }
[Column(TypeName="image")]
public byte Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
}
The compiler doesn't have any issues with the first two annotations but it doesn't seem to like: [Column(TypeName="image")].
Errors:
The type or namespace name 'Column' could not be found.
The type or namespace name 'ColumnAttribute' could not be found.
I'm using Visual Studio 2012 and Entity Frameworks 5.
Any suggestions?
In Entity Framework 4.3.1, ColumnAttribute is defined in System.ComponentModel.DataAnnotations namspace , which is available in EntityFramework.dll. So if you have a reference to that dll and a using statement to the namespace, you should be fine.
In Entity Framework 5, It is in System.ComponentModel.DataAnnotations.Schema namspace, So you need to add a reference to that in your class.
using System.ComponentModel.DataAnnotations.Schema;
You can read more detailed information about it here.
I had the correct using statements ...
I had this problem despite having the correct using statements.
In my case, my project was generated by dotPeek after decompiling a dll (the original source code was lost).
dotPeek created the project with a reference to a copy of EntityFramework.dll just sitting in some folder, not being managed by NuGet.
What worked for me was to remove the reference to EntityFramework, and re-add it using the NuGet console.

Categories