Why aren't my tables showing up in my DbContext? - c#

Long-story-short, if you look at my project you can see that I have an automatically generated model, but after importing the namespace of the mode and trying to use it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LRVault.Models;
using System.Data.Linq;
namespace LRVault.Repositories
{
public class ThreadRepository : VaultCRUD
{
public void AddOrUpdateThreads(IEnumerable<Thread> threads)
{
VaultContext.???
}
}
}
I can't get the tables, i.e. VaultContext.Posts is unrecognized. What am I missing?
EDIT: parent class is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using LRVault.Models;
using System.Data.Entity;
namespace LRVault.Repositories
{
/// <summary>
/// Serves as base class for the CRUD operations
/// </summary>
public abstract class VaultCRUD : IDisposable
{
public DbContext VaultContext { get; private set; }
public VaultCRUD()
{
VaultContext = new LRC_VAULTEntities();
}
public void Dispose()
{
VaultContext.Dispose();
}
}
}

You need to cast VaultContext property to LRC_VAULTEntities, to access the tables:
(VaultContext as LRC_VAULTEntities).
DbContext is the base class of LRC_VAULTEntities, but the 'Tables' are in LRC_VAULTEntities.

You need to create an instance,
using(var context = new VaultContext())
{
}

The VaultContext instance is declared with type DbContext which has nothing to do with your table, you have to change its type to be LRC_VAULTEntities, do this and you will get your tables as you want in the inherited classes

Related

ASP.NET Core richtexteditor devexpress blazor

My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DevExpress.AspNetCore.RichEdit;
using DevExpress.AspNetCore;
using DevExpress.Web.Mvc.UI;
using DevExpress.Blazor.RichEdit;
using System.Runtime;
namespace TextEditor.Pages
{
public static RichEditBuilder RichEdit(this BuilderFactory factory,
string name
);
}
I get these errors:
Error CS1106
Extension method must be defined in a non-generic static class
Error CS0116
A namespace cannot directly contain members such as fields or methods
Error CS0501
'.RichEdit(BuilderFactory, string)' must declare a body because it is not marked abstract, extern, or partial TextEditor
You need to have a public static class RichEditUtils which then contains this method of yours - you cannot have that method directly in the namespace level...
namespace TextEditor.Pages
{
public static class RichEditUtils
{
public static RichEditBuilder RichEdit(this BuilderFactory factory, string name)
{
// do something here that ends up returning a "RichEditBuilder" ...
}
}
}

Access to a Public Variable in another class / folder

I am trying to access to a public static variable in a class but I don't know how to do it. I try different solutions like using and namespace but doesn't work.
In the form called "CategoriasCaracteristicas.cs" (blue), I am trying to access to empresaGlobal.cs (red) as you can see in the next image:
But I can't do it using namespace, or others. The code of the file "CategoriasCaracteristicas.cs" that is where I am trying to access the other class is the following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Utils;
namespace imlnet
{
public partial class CategoriasCaracteristicas : Form
{
private string codEmp;
private string cadenaConexion;
public CategoriasCaracteristicas()
{
And this is the code of empresaGlobal.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ImeApps
{
public static class empresaGlobal
{
public static string empresaID = "3";
public static String EmpresaID
{
get { return empresaID; }
set { empresaID = value; }
}
}
}
Thanks in advance!
Put your empresaGlobal.cs file in a project and add that project as a reference to the GestorCategoriasCaracteristicasIngenieria project.
Make sure any classes, properties and methods you need to access are available using the public keyword.

Converting VB.Net to C# -type or namespace not found

I am converting a VB.Net project to C#. I declare some public structures in one module and access them in another. I get a "The type or namespace name 'ItemInfo' could not be found." - where ItemInfo is one of the structures. Here is code snippet where I declare the structure:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Data.Common;
using System.IO;
using System.Net.Mail;
using dbAutoTrack.DataReports;
using dbAutoTrack.DataReports.PDFExport;
namespace PlanSchedule
{
static class PlanScheduleBLL
{
#region "Structures"
public struct ItemInfo
{
// LinearProcessTime = sum of bll level resource requirements;
// when more than one resource on a level, uses max time required;
// not saved in database, used in order processing only
public string ItemNumber;
public string Description;
public string MakeBuy;
public long TotalShelfLife;
public long ReceivedShelfLife;
public float YieldPercentage;
public float BatchSize;
public float SafetyStock;
}
...and this is where I reference it:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
namespace PlanSchedule
{
public partial class BuildToStockOrderEntry
{
// slItemList
// Key = <item number> - <item description> Value = item number
private SortedList<string, string> slItemList = new SortedList<string, string>();
// slItems
// Key = item number Value = item info
private SortedList<string, ItemInfo> slItems = new SortedList<string, ItemInfo>();
I do not know C# so I may have a problem in my syntax. Is my declaration of the SortedList using ItemInfo correct?
TIA,
John
You currently have the ItemInfo struct nested in the static class PlanScheduleBLL. You have two options:
Pull it out of that class, and just into the namespace.
If you really want it nested, you need to make PlanScheduleBLL public, and refer to it like PlanScheduleBLL.ItemInfo
It's because you are declaring the struct inside another class. You could reference it by doing:
private SortedList<string, PlanScheduleBLL.ItemInfo> slItems ...
But a much better approach would be to just define the struct outside of the class and directly in the namespace itself (i.e. get rid of the PlanScheduleBLL static class).
Place the ItemInfo structure outside of the PlanScheduleBLL class.

Error when trying to create a partial class

I have created a Area class using Linq-to-SQL.
Now I want to create a partial class of the same name so I can implement validation.
Error 1 Cannot implicitly convert type
'System.Data.Linq.Table<SeguimientoDocente.Area>'
to
'System.Linq.IQueryable<SeguimientoDocente.Models.Area>' C:\Users\Sergio\documents\visual
studio
2010\Projects\SeguimientoDocente\SeguimientoDocente\Models\AreaRepository.cs 14 20 SeguimientoDocente
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SeguimientoDocente.Models
{
public class AreaRepository
{
private SeguimientoDataContext db = new SeguimientoDataContext();
public IQueryable<Area> FindAllAreas()
{
return db.Areas;
}
public Area GetArea(int id)
{
return db.Areas.SingleOrDefault(a => a.ID == id);
}
public void Add(Area area)
{
db.Areas.InsertOnSubmit(area);
}
public void Delete(Area area)
{
db.Areas.DeleteOnSubmit(area);
}
public void Save()
{
db.SubmitChanges();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SeguimientoDocente.Models
{
public partial class Area
{
}
}
Here's a screenshot.
This is almost certainly because your partial class is not in the right namespace. Go into the .designer.cs file of the LINQ model, look for the generated Area class, and make sure the namespace you wrapped your partial class in matches.
EDIT
I just fixed the formatting in your question. The error message does in fact indicate that your partial class is in the wrong namespace.
Error 1 Cannot implicitly convert type
'System.Data.Linq.Table<SeguimientoDocente.Area>'
to
'System.Linq.IQueryable<SeguimientoDocente.Models.Area>'
As you can see from the error above, you need to change the namespace your partial class is in to be SeguimientoDocente, not SeguimientoDocente.Models. As it stands now, they are two completely different incompatible types that happen to have the same simple name.
The error message tells you that the problem is in line 14 of the AreaRepository.cs file. Specifically, you are trying to return db.Areas from a method whose return type is IQueryable<Area>, though db.Areas is in fact of type System.Data.Linq.Table.

How do I add common C# code to a Visual C# 2008 Express project/solution?

(I still feel like a complete newbie in MS Visual environments... so please bear with!)
I'm using Microsoft Visual C# 2008 Express Edition.
I have a project and in that project are two different forms. The .cs file for each form starts out:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyNameSpace
{
public partial class MyFormName : Form
{
...
(...and the second is "MyFormName2" but no differences besides that)
I want to write a function that I know both forms are going to need to access. I right-clicked on my project, selected "Add", selected "New Item" then selected "Code File" and named my file "Common.cs" and it gave me a completely blank file that's in my project.
How do I set this up...? I thought I should do the following...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyNameSpace
{
}
...but then when I try to add a function like:
public void mytestfunc() {
}
within that namespace I get the following error:
"Expected class, delegate, enum, interface, or struct"
How do I set things up so I can have "mytestfunc" be available to both MyFormName and MyFormName2?
Thanks!
-Adeena
UPDATE:
Understand (now) that everything must be in a class, but then I don't understand how to really use it. Does that mean I have to create an object? This common function happens to just be some math...
so now if I have this:
namespace MyNameSpace
{
public class MyCommonClass
{
public void testFunc()
{
MessageBox.Show("Hee hee!");
return;
}
}
}
...how do I call testFunc from my Form? Must I do the following:
MyCommonClass temp = new MyCommonClass;
temp.testFunc();
or is there another way to call testFunc?
If you do something like:
namespace MyNameSpace
{
public class myclass
{
public myMethod()
{
// Code
}
}
}
You will be able to instantiate and access it. If you change it to:
namespace MyNameSpace
{
public class myclass
{
public static myMethod()
{
// Code
}
}
}
You will be able to call myClass.myMethod without instantiating a new myClass.
The short answer is that everything needs to be inside a class; I'd suggest you sit down with a basic tutorial to help you get to grips with the basics...
Code need to be inside classes.
It would look something like this:
using System;
namespace MyNameSpace
{
public class CommonHelper
{
public string FormatMyData(object obj)
{
//do something
return String.Empty;
}
}
}
If the function you call is not related to the forms, make it static
namespace myns
{
public static class myhelper
{
public static void DoSomething()
{
}
}
}
and call the method using myhelper.DoSomething();
If the function you want to call is somehow form-related, e.g. common functionality across multiple forms, derive a class from Form (does not need a visual form) and make it base class of the visual forms:
namespace myns
{
public class MyFormBase : Form
{
protected void DoSomethingWithTheForm()
{
}
}
}
and in your form's .cs:
namespace myns
{
public partial class MyFormName : MyFormBase
{
}
}

Categories