|
in many mvc application i have seen people use virtual keyword. i like to know the reason.
i have seen a video from this url https://www.youtube.com/watch?v=BSgAA8y5ViU which generate UI from model with scaffolding
so see this code first
public class Student
{
public int StudentId;
public string StudentName;
public int CourseId;
public virtual Course Courses { get; set; }
}
public class Course
{
public int CourseId;
public string CourseName;
public string Description;
public ICollection<Student> Students {get;set;}
public ICollection<Lecture> Lectures { get; set; }
}
public class Lecture
{
public int LectureId;
public string LectureName;
public int CourseId;
public virtual Course Courses { get; set; }
}
1) why lecture and student class has common property called CourseId ?
2) see below code
public ICollection<Student> Students {get;set;}
public ICollection<Lecture> Lectures { get; set; }
why they use ICollection ? they can use here list etc ?
3) why Courses property has been declared with virtual keyword in lecture and student class ?
please help me to understand the real use of virtual keyword
public virtual Course Courses { get; set; }
in what kind of scenario we use virtual keyword ? what is advantage ?
please explain the reason why people use virtual keyword with EF ?
thanks
tbhattacharjee
|
|
|
|
|
|
In Entity Framework, properties are declared as virtual to allow for change-tracking, and for lazy-loading of related entities. EF dynamically generates a proxy class derived from your POCO class to add these features, and needs to override the properties to do so.
Requirements for Creating POCO Proxies[^]
The CourseId and (incorrectly named) Courses properties are used to define a one-to-many foreign-key relationship to the Course entity:
Entity Framework Relationships & Navigation Properties[^]
The relationship between students and courses doesn't make logical sense - one student can currently only take one course. In a real example, I would expect to see a many-to-many relationship.
The type of a navigation property must implement ICollection<T> to allow for lazy-loading. Whilst you could use IList<T> , it usually doesn't make sense, since the related entities don't have a defined order. (You could also use a concrete type, but it's generally better to stick with an interface.)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
"virtual" WAS supposed to reflect that this particular property was a "navigation" property; I could probably find a reference if I tried; but I do not think there was ever any "practical" explanation.
I would omit the keyword and still wind up with relationships simply based on names; I had to explicitly resort to "ignoring" those properties to keep EF from "assuming" things.
Same with ICollection; it's usually easier to simply always use Lists, if you plan to use LINQ anyway.
Virtual, ICollection AND "partial classes" are all about "extending" basic POCO's; and not any particular EF magic, IMO.
Oh; and I control "lazy loading" via EF dbContext configuration options and run-time calls; not any C# "keywords".
|
|
|
|
|
Hello,
I am developing an application which triggers when there is a windows explorer window in foreground. My application triggers a window (form) which will be placed on screen near the opened windows explorer (Planning to keep it just below the search option).
I am not opening the windows explorer in my code, instead user will open windows explorer and I will trigger my application whenever there is a windows explorer in foreground.
Here is my code,
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
public class Form1
{
private void Form1_Load(System.Object sender, System.EventArgs e)
{
Timer1.Interval = 100;
Timer1.Start();
}
private void Timer1_Tick(System.Object sender, System.EventArgs e)
{
dynamic hWnd = GetForegroundWindow();
if (ClassName(hWnd) == "CabinetWClass") {
Timer1.Stop();
using (Form2 f2 = new Form2()) {
f2.ShowDialog();
}
}
}
#region "API"
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 GetClassName(System.IntPtr hWnd, System.Text.StringBuilder lpClassName, Int32 nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetForegroundWindow();
private string ClassName(IntPtr hWnd)
{
StringBuilder sb = new StringBuilder(256);
GetClassName(hWnd, sb, 256);
return sb.ToString;
}
public Form1()
{
Load += Form1_Load;
}
#endregion
}
But I am not getting anything to get the window position of foreground "windows explorer" window.
Is there any way to read the position of current foreground “Windows Explorer” window using .net?
modified 16-Aug-16 4:58am.
|
|
|
|
|
This probably isn't going to work quite the way you want, due to the way Windows Explorer works - it doesn't always open a new top-level process for an instance. At the moment I have two Windows Explorer processes: one with a single Main window (and the title is the folder name) and one with two sub windows (and it's title is an empty string). How I got there? Depends on how you open the window. The "one off" one was opened by an app, the other two were opened from the task bar...
But you probably want to start here:
Process[] all = Process.GetProcesses();
foreach (Process p in all)
{
string name = p.ProcessName;
IntPtr hWnd = p.MainWindowHandle;
string title = p.MainWindowTitle;
if (p.ProcessName.Contains("explorer"))
{
Console.WriteLine("{0}:{1}", p.ProcessName, p.MainWindowTitle);
}
}
The handle should give you the location of the main window (but you may need to do some delving to find "combined" windows)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
sorry for the confusion, I have edited the question. I facing trouble in getting window position of windows explorer.
|
|
|
|
|
srikrishnathanthri wrote: dynamic hWnd = GetForegroundWindow();
No need to declare hWnd as dynamic ; just declare it as IntPtr . You're not using any dynamic resolution[^] on the variable, so you're just slowing your code down for no reason.
If you want to find the position of a window, you need to call GetWindowRect :
GetWindowRect function (Windows)[^]
GetWindowRect: pinvoke.net[^]
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public System.Drawing.Point Location
{
get { return new System.Drawing.Point(Left, Top); }
}
}
[DllImport("user32.dll", SetLastError=true)]
private static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
private static System.Drawing.Point GetWindowLocation(IntPtr hWnd)
{
RECT lpRect;
if (!GetWindowRect(hWnd, out lpRect)) return System.Drawing.Point.Empty;
return lpRect.Location;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I want to set the autocomplete to the textbox using LINQ to entities.
This is my code:
using (Reference_TraductionEntities context = new Reference_TraductionEntities())
{
var source = new AutoCompleteStringCollection();
var name = from a in context.Feuil1Prenom
where a.PRENOMF.StartsWith("i")
select a.PRENOMF;
source.AddRange(name.ToArray());
textBox1.AutoCompleteCustomSource = source;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
This code is OK , but just with the character "i ", I want autocomplete with any character entry in textbox
How can i fix it ?
Thanks,
|
|
|
|
|
Change StartsWith("i") to actually contain the text the user is typing.
This space for rent
|
|
|
|
|
Very Thanks Pete O'Hanlon !!!
I replace starts with "i" to starts with textbox1.text ...
It works !!!
|
|
|
|
|
You're welcome. Glad you got it sorted.
This space for rent
|
|
|
|
|
hi guys i am converting java code to use sentiwordnet
tempDictionary.get(synTerm).put(synTermRank,
synsetScore);
that line in java how we can write in C#.
i triedlike tthat tempdict[synterm].Add(syntermrank, sysnsetscore);
|
|
|
|
|
|
Does anyone has accounting software source code which includes trial balance, profit & loss a/c and balance sheet??
|
|
|
|
|
Yes
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes, many software and finance companies. However, they are unlikely to give it to anyone for nothing.
|
|
|
|
|
This is not a proper question for this site (hence the responses you already got), I would encourage you to either make a specific question or google for what you are looking for. Or just navigate through sourceforge or github.
If you think you can do a thing or think you can't do a thing, you're right - Henry Ford
Emmanuel Medina Lopez
|
|
|
|
|
Background
I'm working with a special type of "offline files"/"File sync"-system, and need to compare a users files on his/her computer en on the file server. The place I'm working at, was tricked by a Microsoft rep back in 2011, to use a system that works kind of like OneDrive. The user have their files on their laptop, and when connected to the organization's network, the files are synched with the file server
The situation
Now that they are migrating to Windows 10, and actual OneDrive, I've discovered a problem: the files aren't always synched. The process is now to run a set of tools on the users' computers,to check for errors, and if there is, (and there's plenty of errors), the user need to confirm which file is the correct one.
The problem
I've been testing WinMerge, and similar programs, but they are mostly garbage for the use I need them for. I need flexibility, meaning that i need complex filtering options, (e.g. folder names to increase processing speed), but also the options to look at files in different manners, (hashes of files, metadata, or byte-for-byte). THis could actually be done in powershell pretty "easily", however, the user will have to interact with this in a meaningfull and friendly way, so an interface is therefore needed, which allows for sorting, opening selected files, copying, copy to third party location, copy to same location with a pre/su-fix. Options to reduce timeconsumption, (quick mode which only checks metadata, or hashmode, which is in the middle, or slow mode byte-for-byte)
My solution (thus far)
Create this as a C# program, with a nice interface, (I've made the interface, and it's actually very nice), I've tested some simple file comparisons, but I need you guys' input. Am I doing this right or is there a piece of software that I haven't found yet??
Thanks for all input 
|
|
|
|
|
I wrote some software that did this back about 2010 and originally used the Sync Framework to do the work, but ended up hand rolling so much of my own code that there's very little original SF left in there. The approach I took was more than you'll need to do because you're looking at, effectively, a one time operation but my solution took care of client/server/server syncing - anyway, the crux of the checks - you check the file lengths first - if they are different, you don't need to do any further checking as you have a potential problem file; then you create a hash of both files, if they don't match, you have files that need checking. The vast majority of cases are caught by the file length as it's comparatively rare that you'll end up with different files that are the same size.
This space for rent
|
|
|
|
|
Why don't you use the aforementioned Powershell to run a scan/replace script and toss that into NETLOGON. Seems like that would be easier and more sensible, as it sounds like you're using organizational shares.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
What is described here applies to both VS 2013 and VS 2015
Consider the case when one wants to make a sub-classed Control (using one of the standard MS provided WinForm Controls) as a separate Project: so that you can end-up with a nice .dll that you can then mount on Visual Studio's ToolBox for handy re-use.
The only relevant new Project Type available is Windows Forms Control Library which when selected gives you an initial Class definition like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestWinFormControlLibrary
{
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
}
} And you, foolish mortal, think you could easily rename the Project and change the Class code to something like this:
public partial class CustomTextBoxForWhatever: TextBox while still leaving the Project Output Type as it is (Class Library).
That won't compile because the call to 'AutoScaleMode in the Designer.cs file will fail because you have stumbled across the assumption VS makes that you are building a ContainerControl, while a sub-classed TextBox does not expose that Type.
Okay, so you take out the call to 'AutoScaleMode, and now your design view switches to no-view ... just the type of screen that you see when you a new Component to a WinForm project. But, you do have, indeed, a sub-classed Control to work with, compile, and then mount in the ToolBox as you wish.
Now, maybe (?) the designers of VS did not assume someone would want to start designing a custom Control by first defining the Control, and then adding another Project to the Solution of Type Windows Form in order to test the Control as it is developed.
If you start a Project of Type WinForms, you can then add a "Custom Control" to it, but then that Control will be embedded in a WinForms Project.
Do you see what I am getting at ? Have I missed something obvious ... again ?
Since you can stick a Control of whatever Type in the ContainerControl Prototype that a Project of Type WinForms Control Library builds-out for you ... is this a non-issue for most mortals ?
Why do I quibble as the executioner asks if I will have the blindfold: usually, if I am going to do this ... and I do ... do this ... I am going to override methods of the sub-classed Control for various reasons.
Could CodeProject not groan under the weight of one more article or tip-trick describing all ... this ?
morituri te salutamus, Bill
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
modified 11-Aug-16 7:33am.
|
|
|
|
|
Would not be the first time that I had to code constructors / initializers to account for the behavior of a designer when loading a control.
I live with a given designer's behavior, grateful that I'm aware of it, and override it at run-time (DLL call) when necessary.
|
|
|
|
|
we're loading all partials views to a common div.
$(divname).load(url);
on clicking a row in a table or clicking on an icon,or clicking on link, view changes. my requirement is, in a partial view, we have two text boxes which are free type. before this view clears, i need to save these data to db. what appropriate event is to be used before this view goes? i tried $(document).unload,$(document).unbind,$(div).change,$(div).unload etc. nothing worked. please help me to resolve this issue
|
|
|
|
|
You do realise that your question has nothing to do with C# don't you? When asking a question, it's appropriate to choose an appropriate forum; in your case, it's probably best to post here[^].
This space for rent
|
|
|
|
|