I'm using VS2010 with Entity Framework (file version is 4.4. product version is 5)
I have installed the EF5.x DbContext generator.
After creating my .edmx file, I right clicked on the empty space and added a new DbContext template, which generated the context.tt and .tt files.
However, in the .tt files, this is how the auto generated code looks like:
namespace DataObjects.EntityFramework.Models
{
using System;
using System.Collections.Generic;
public partial class SubSystem
{
public string SubSystemId { get; set; }
public string Description { get; set; }
public string Fmode { get; set; }
public Nullable<System.DateTime> LastBackup { get; set; }
}
}
The problem is that the using statements are inside the namespace, which gives rise to a compilation error.
Those compilation errors must be related to something else, because it's perfectly legal in C# to have using statements in the namespace.
Verify that you've added all of the correct references, such as EntityFramework.dll
Related
I am creating an app that uses a database (SQLite). I am using entity framework and ADO.NET to interact with it.
I have a seprate Models project in my app that contains all of my Database models.
Now I want to mark some of my class properties as required to reflect the "NOT NULL" option in my database. But if I add the [Required] Attribute from DataAnnotations namespace I get a compiler error saying it cannot be resolved.
Here is how my class looks like :
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ReflowModels
{
public class Tag
{
public Tag()
{
this.Options = new HashSet<Option>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
public ICollection<Option> Options { get; set; }
}
}
I have also added a reference to EntityFramework.dll in my project.
you need to add this to your using block
using System.ComponentModel.DataAnnotations.Schema;
you need to add this to your using block
using System.ComponentModel.DataAnnotations;
In case it still doesn't work, maybe you should add it to your References
When I try to add Controller using build-in generator I get following error message:
Where "Brand" is a model class and "ApplicationDbContext" is my data context class.
I never encoutered error like this before. It does not occur all the time (2 success vs 30-40 attempts without changing single line of code). I was able to create Controller for same class minutes ago, but it's really annoying when you try to add something new and try more than 10 times with no results.
Of course, I could write my own controller and views, but I really like using included tools.
I have already repaired VS using "Modify" option in "Add or remove". I'm using latest Microsoft Visual Studio Community (MVSC2015 V14.0.25425.01 Update3).
EDIT:
Brand.cs:
namespace AppBrander.Models
{
public class Brand
{
public int ID { get; set; }
public string Email { get; set; }
public string BrandName { get; set; }
public string CompanyName { get; set; }
public string Description { get; set; }
}
}
Works in brand new project, fails in old - I guess it's just my VS installation being totally messed up. I should have been guessed, but I just did second repair...
I am used to VB and is kind of new to C# syntax. But from what I read everywhere the syntax seems to be kind of identical for Partial Classes.
I am trying to make a partial class to a generated partial class generated with Entity Framework.
It seems like my class can't reach the other one.
The error:
'XmasMVC.CmsSystemPagePart' does not contain a definition for
'DefaultLanguageCode' and no extension method 'DefaultLanguageCode'
accepting a first argument of type 'XmasMVC.CmsSystemPagePart' could
be found (are you missing a using directive or an assembly reference?)
I can compile but then I get errors while running. Seems like my class overwrites the generated one.
This is the genereated class from EF.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace XmasMVC
{
using System;
public partial class CmsSystemPagePart
{
public string DefaultLanguageCode { get; set; }
public string PagePartName { get; set; }
public string Description { get; set; }
public string DefaultLangValue { get; set; }
public string LanguageCode { get; set; }
public string Value { get; set; }
}
}
This is my class.
namespace XmasMVC
{
using System;
public partial class CmsSystemPagePart
{
public string GetValue()
{
if (this.Value == null)
return this.DefaultLangValue;
else return this.Value;
}
public string GetLanguageCode()
{
if (this.Value == null)
return this.DefaultLanguageCode;
else return this.LanguageCode;
}
}
}
Found this "Warning":
"The type 'XmasMVC.CmsSystemPagePart' >in 'C:\VSS\Comdigit\Xmas\XmasMVC\App_Code\CmsSystemPagePart.cs' conflicts with the imported >type 'XmasMVC.CmsSystemPagePart' in 'C:\VSS\Comdigit\Xmas\XmasMVC\'. Using the type defined >in 'C:\VSS\Comdigit\Xmas\XmasMVC\App_Code\CmsSystemPagePart.cs"
This is how the Objects Browser looks like. Strange for me!
This link here explains that
In general, ASP.NET creates an assembly for each application directory (such as App_Code) and one for the main directory.
For your case, although the partial classes are in the same project and the same namespace they are not being compiled into the same assembly.
Try moving the source files out of App_Code and make sure their Build action is Compiled in the properties window.
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.
I am working in .NET Entity Framework 4.0
I am using viewstate to save an entity. And I have serialize that entity as well. But when I try to save data to viewstate, getting this error:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Error serializing value 'System.Collections.Generic.List`1[Pc.PrecisionCare2.ModelTypes.Medication]' of type 'System.Collections.Generic.List`1[[Pc.PrecisionCare2.ModelTypes.Medication, PrecisionCare2ModelTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].'
Maybe this will help anyone. I wanted to serialize a entity in the Viewstate myself and couldn't find a good solution (XMLSerialization, Byte Serializing, DataContract). What I found out is that I could "extend" the code generated classes (they are partial) and make it Serializable.
For example this is a .net code generated entity:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace OAuthServerBroker.EF
{
using System;
using System.Collections.Generic;
public partial class ResourceOwner
{
public ResourceOwner()
{
this.Grant = new HashSet<Grant>();
}
public System.Guid ResourceOwner_ID { get; set; }
public string ResourceOwner_Username { get; set; }
public virtual ICollection<Grant> Grant { get; set; }
}
}
When I create a new class file with the same class name and namespace I can make the Entity Serializable :).
using System;
namespace OAuthServerBroker.EF
{
[Serializable]
public partial class ResourceOwner
{
public EntityState State { get; set; } //can even put into new properties
}
}
Hope this might help anyone since it's an old post :(.